0

Why does 20150813.00 convert to 20150812.000000 and 20150708.000000 convert properly to 20150708.000000?

Code Sample:

#include <stdio.h>
#include <iostream>
int main()
{
        double one_o = 20150813.00;
        double two_o = 20150708.00;
        float one = (double)one_o;
        float two = (double)two_o;
        std::cout << std::fixed << "one_o = " << one_o << " one = " << one << std::endl;
        std::cout << "two_o = " << two_o << " two = " << two << std::endl;
        return 1;
}

Output is --

one_o = 20150813.000000 one = 20150812.000000
two_o = 20150708.000000 two = 20150708.000000

Help me understand double to float type conversion!

Edit: std::numeric_limits<float>().max() reports 340282346638528859811704183484516925440.000000 as the maximum to store in a float, which is far beyond what I'm attempting to store:

M.M
  • 138,810
  • 21
  • 208
  • 365
DumbCrabby
  • 23
  • 2
  • 1
    maybe the number exceeds the maximum size of a float number – Zachi Shtain Aug 15 '15 at 09:46
  • float max = 340282346638528859811704183484516925440.000000 Found with the following: std::cout << "float max = " << std::numeric_limits().max(); Still don't understand how this answered in the other post.. – DumbCrabby Aug 15 '15 at 09:52
  • Pick up your favorite language specification, and look for information about accuracy – Amit Aug 15 '15 at 09:55
  • See also [this question](http://stackoverflow.com/q/7356741/33499) – wimh Aug 15 '15 at 09:55
  • It's just a value that I'm trying to store inside a float as a float for further processing. – DumbCrabby Aug 15 '15 at 09:56
  • 1
    @Amit Which will then just refer to the [IEEE-754](https://en.wikipedia.org/wiki/IEEE_floating_point) specification, so we can avoid the intermediate step. – Voo Aug 15 '15 at 09:56
  • The last digit is at the extreme end of what can and what cannot be stored in a float. – Jongware Aug 15 '15 at 09:57
  • Wimmel -- Thanks, but it really doesn't address the question. To my understanding the rounding/truncation only occurs when you have a remainder, in this case I'm just trying to store a whole number in a float (with no decimal places) – DumbCrabby Aug 15 '15 at 09:58
  • std::numeric_limits().max() reports 340282346638528859811704183484516925440.000000 as the maximum.. which is far beyond the number I'm trying to store? – DumbCrabby Aug 15 '15 at 09:59
  • You can store `340282346638528859811704183484516925440.000000` *but not* `340282346638528859811704183484516925439.99999`, even though it is smaller. – Jongware Aug 15 '15 at 10:01
  • @DumbCrabby Normal numbers are represented as (1.f)*2^(e-127) in IEEE-754 floating point math and you only have limited range for both f and e. Doubles have larger possible values for f and e, so you can get numbers you can't have with floats. – Voo Aug 15 '15 at 10:02
  • see algorithm of conversion from double to float in this [question](http://stackoverflow.com/questions/16737615/how-is-actually-done-floating-point-conversion-double-to-float-or-float-to-doub) – Alireza Jafari Aug 15 '15 at 10:02
  • As an aside, storing dates in a floating-point format seems ridiculous... – Lightness Races in Orbit Aug 15 '15 at 13:06
  • http://floating-point-gui.de/ – Steve Aug 15 '15 at 16:37

1 Answers1

4

float can store much bigger numbers than 20150813, but it cannot store big integers precisely.

Assuming IEEE 754 is used, the single precision float can store integers precisely at most 224, which is 16777216. Integers bigger than it will probably lose some precision.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294