r/shittyprogramming • • Jun 28 '15

Embedded developer adventures

Post image
297 Upvotes

55 comments sorted by

View all comments

35

u/say_wot_again Jun 28 '15

But.... That's not what they meant when they said double! And how would you tell 1, 10, and 100 apart? I call shenanigans.

0

u/[deleted] Jun 28 '15

[deleted]

6

u/thorium220 Jun 28 '15

All you'd have to do is get a pointer to it and then you could write two integers into it. As long as you remember what it is and you read it appropriately, it's a very efficient way of sending two integers around.

I've personally made excellent use of a library that masks integers as 16.16 fixed-point reals. Very useful when you've got, say, a 10-bit ADC and a bunch of maths to do, but not enough RAM or processing time between samples to work in floats.

7

u/ZugNachPankow Jun 28 '15

But why not use a struct? They are literally meant to be a data structure to store several pieces of data of several types.

5

u/thorium220 Jun 28 '15

Honestly, I have no idea why you'd perform this particular 'hack', I'm just saying that for this particular instance it probably gave some small speed or memory advantage.

I agree, it shouldn't make a difference.

1

u/stone_henge Jun 28 '15

That would complicate mathematical operations on the numbers quite a bit. With fixed point representation, add is still add, subtract is subtract and multiplication is multiplication and right shift. This also allows effective conversion between and operations on numbers with different radix points.

I am not sure why you would approach the integer and fractional parts as different datatypes. Just name a new type for the whole thing to be explicit about it.

0

u/[deleted] Jun 28 '15

[deleted]

1

u/thorium220 Jun 28 '15

In general, I'd agree, I'm just saying that I can understand how a situation such as the OP described could come about. Maybe the developer was running low on RAM and decided to use a float-only register to store these two ints in?