You're skipping a lot of floating-point numbers every time you double your max value. Your code computes the largest representable power of two, not the largest representable Float64. The general representation <http://en.wikipedia.org/wiki/Double-precision_floating-point_format> of a Float64 is:
1 bit sign 11 bits of exponent 52 bits of digits after the leading 1 (which is implied) Your code starts with 1.0 which has these bits: 0 01111111111 0000000000000000000000000000000000000000000000000000 This decodes as 1.0*2^(1023-1023) – i.e. one. When you double it, you get 2.0, which is 0 10000000000 0000000000000000000000000000000000000000000000000000 As you can see, the data part is still all zeros and the only thing that happened is that the exponent 1024 instead of 1023. This keeps happening each time you double. Soon you get to 8.98846567431158e307, which has these bits: 0 11111111110 0000000000000000000000000000000000000000000000000000 The next exponent value is 0b11111111111 == 2047, which is only used to represent infinities and NaNs so the next time you double, the correct value would be larger that is possible to represent, so you get Inf. This is where your code stops. However, there are lots more values bigger that this that don't have all zero data – you can get *almost* twice as big. In particular, we would expect that this representation to be the largest finite value: 0 11111111110 1111111111111111111111111111111111111111111111111111 Indeed, that is exactly the bits of 1.7976931348623157e308: julia> bits(realmax()) "0111111111101111111111111111111111111111111111111111111111111111" As expected, this is almost – but not quite – twice as big as 8.98846567431158e307: julia> 1.7976931348623157e308/8.98846567431158e307 1.9999999999999998 I can think of a few ways to experimentally find the maximum Float64, but it's hard to reconcile mentally because I can just write the bits of the number down :-). It's exactly this: julia> reinterpret(Float64, 0b0111111111101111111111111111111111111111111111111111111111111111) 1.7976931348623157e308 Also, you can just use integer arithmetic to find the float right before Inf: julia> reinterpret(Float64, reinterpret(Uint64, Inf) - 1) 1.7976931348623157e308 Anyway, that's probably more explanation than anyone needed but I couldn't resist. On Sun, Oct 26, 2014 at 4:38 PM, Beata Szostakowska <[email protected]> wrote: > I was trying to experimentally find the maximum number for type float64. > The number I got is 8.98846567431158e 307. However built-in function > realmax(Float64) returns 1.7976931348623157e308. What is the reason of > this difference? > This is my code to find maximum number: > > temp = 1. > max = 1. > > while !isinf(max) > temp = max > max *= 2. > end > > max = temp > > >
