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.

20

u/amphetamachine Jun 28 '15

Cast it as a string and pass it to atof

9

u/immibis Jun 28 '15

You mean like this?

int getRightPartOfDecimal(double d) {
    // XXX 2015-06-28 H.K. - /u/amphetamachine suggested this method to extract the second part of a double
    // XXX 2015-06-28 H.K. - Not sure why this needs an int cast in between though
    return atof((char*)(int)d);
}

2

u/ThisIsADogHello Jun 29 '15
     // XXX 2015-06-28 H.K. - /u/amphetamachine suggested this meth
od to extract the second part of a double

Interesting place for a line break to appear...

2

u/immibis Jun 29 '15

What browser breaks lines in the middle of words?

1

u/ThisIsADogHello Jun 29 '15

Apparently Chrome will, if it's in a <code> tag. I'd rather have that than having to sidescroll, though.

1

u/amphetamachine Jun 29 '15

Chrome could at least let us set sbr.

1

u/amphetamachine Jun 29 '15

Oops, I guess all that higher-level programming has ruined me. By "cast as a string" I really meant sprintf(strval, "%f", floatval)

5

u/alienangel2 Jun 28 '15 edited Jun 28 '15

Presumably he meant they were using the high bytes of the double for one integer and the low bytes for another, not storing string representations (which wouldn't fit in a double anyway). So the top 4 bytes of a 64bit double would store 0 as a 32bit int, and the lower 4 bytes would store 10 as another 32bit int, and there'd be no issue differentiating between 1, 10 and 100.

Still a bit pointless unless the system they're using doesn't have a compiler with things like uint32 defined, but that might actually be the case for some architectures.

If he's reading it right and it really is storing the values on sides of the decimal that is extra bizarre, since doubles are floating point. I don't think anyone would jump through the hoops needed to try to make that work, so yeah he's either making it up or misreading.

Anyway, it's not like shitty devs don't exist all over, op would find other retardedness outside of embedded. The int inside doubles thing might be justified though (actually if the c compiler is homebrewed for some obscure chip, even the code in header files might be justified). Given that OP has two weeks of experience, he probably doesn't understand what is going on enough to judge what really is bad in an embedded system and what is due to compiler or hardware idiosyncrasies.

1

u/fb39ca4 Jun 29 '15 edited Jun 29 '15

No, it sounds shittier than that. By either side of the decimal point, I think it meant this:

Let A and B be the positive integers you want to store. Store them as A + 1/B in a floating point number. To extract them, A = integer part, and B = 1/fractional part. (If B = 0, don't put anything in the fractional part.)

For example, 10 and 7 would become 10.142857... A = 10, and B = 1/.142857.

If you wanted to store signs, you could use the sign bit from the floating point number for one, and then for the other, remap the integers so that positive integers become positive even integers and negative integers become positive odd integers.

1

u/alienangel2 Jun 29 '15

Yeah you can come up with mappings that can be interpreted as storing the two numbers in the two parts. But to do so you have to understand floating point representation to begin with, and and I don't think anyone who does would try to store two ints like this, rather than just writing them directly into the high and low bytes for the double.

The most likely thing IMO is that the guy posting about it didn't understand what he was reading.

1

u/fb39ca4 Jun 29 '15

This is /r/shittyprogramming so I am just going to assume it is shitty. But the method I described would work for any floating point format as long as it has sufficient precision.

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?

1

u/apocryphalmaster Jun 28 '15 edited Jun 28 '15

Or just write one of them backwards?

1

u/oddark Jun 28 '15

Why has no one considered that the numbers are padded on the left with zeros?

If you want to store 0xF01 and 0x42, the result would be 0xF01.0042. Just take the decimal part and multiply it by 0x10000.

1

u/fb39ca4 Jun 29 '15

If you wanted to store two integers in a floating point number, you could store one as the integer part of the number, and the other in the fractional part as its reciprocal. Of course, you can only store one of the numbers signed that way.