Live data from Hacker News

Decoding Floats

raysohn.com

1–6 of 6 posts

Re: Decoding Floats

#2
When dealing with floats, it should be noted that unlike a 32-bit `int` which is a signed 2's complement representation; a 32-bit `float` follows single precision floating point representation which is 1 sign bit, 8 exponent bits and 23 mantissa bits. Not that there's anything wrong with the article, i just wanted to put it here.

Re: Decoding Floats

#3
In college, I remember we had to manually encode / decode a binary float to a decimal float as exercises. While it was rather fun to understand how machines store a float it could get messy with 64 bits. Also, completely ridiculous tasks.

[Wrong!] Nitpick from the article: the main is declared as returning an int and there is nothing returned.

Re: Decoding Floats

#4

In college, I remember we had to manually encode / decode a binary float to a decimal float as exercises. While it was rather fun to understand how machines store a float it could get messy with 64 bits. Also, completely ridiculous tasks. [Wrong!] Nitpick from the article: the main is declared as returning an int and there is nothing returned.

Not return-ing but just falling through to the closing } is perfectly fine and defined as returning 0.

Re: Decoding Floats

#5
post #4

In college, I remember we had to manually encode / decode a binary float to a decimal float as exercises. While it was rather fun to understand how machines store a float it could get messy with 64 bits. Also, completely ridiculous tasks. [Wrong!] Nitpick from the article: the main is declared as returning an int and there is nothing returned.

Not return-ing but just falling through to the closing } is perfectly fine and defined as returning 0.

To clarify: this is only for main(), in C++: (see c3.6.1p5 of the standard)

"A return statement in main has the effect of leaving the main function (destroying any objects with auto- matic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0"

It's really best not to get in the habit of not returning from a function though, as in the vast majority of cases, you'll get a random value (well, whatever's in the appropriate register) as the return result, and unless you specifically configure it, most compilers won't even warn you!

Re: Decoding Floats

#6
post #4

In college, I remember we had to manually encode / decode a binary float to a decimal float as exercises. While it was rather fun to understand how machines store a float it could get messy with 64 bits. Also, completely ridiculous tasks. [Wrong!] Nitpick from the article: the main is declared as returning an int and there is nothing returned.

Not return-ing but just falling through to the closing } is perfectly fine and defined as returning 0.

Thank you for pointing this out! It is even described in the language's specification:

"If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; 11) reaching the } that terminates the main function returns a value of 0." ( http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf, 5.1.2.2.3 Program termination )