0.2 0.4 0.6000000000000001 0.8 1.0 1.2000000000000002 1.4000000000000001 1.6 1.8 2.0
println(0.2 * 3) does the same thing. not 0.6 but 0.6000000000000001does anyone know why this happens?
julia> a = d"0.2"
+2E-1
julia> for i in 1:10 ; println(a * i) ; end
+2E-1
+4E-1
+6E-1
+8E-1
+10E-1
+12E-1
+14E-1
+16E-1
+18E-1
+20E-1julia>tax = .7 * 0.05
0.034999999999999996
julia> round(tax,2)
0.03
julia> tax = d".70" * d"0.05"
+350E-4
julia> round(tax,2)
+4E-2
As you can see, with binary floating point, you get the wrong answer (0.03), but with decimal floating point, you get the correct answer (the one the tax authorities will want you to give them).
n*2^k where |n| < 2^53 and -1074 ≤ k ≤ 971 (both integers).
3602879701896397*2.0^-55 =
0.1000000000000000055511151231257827021181583404541015625.
eps(1.0) = 2.220446049250313e-16eps(12.34) = 1.7763568394002505e-15eps(1e25) = 2.147483648e9.
I would recommend against using decimal floating-point unless you really must. On almost any hardware you're likely to use, binary floating-point arithmetic will be an order of magnitude faster than decimal floating-point arithmetic. Moreover, if you use decimal floating-point, you will chronically find yourself unable to use standard numerical libraries without converting your data first.
julia> f1() = (a = big"0.0" ; for i = 1:10000 ; a += big"0.1" ; end ; a)
f1 (generic function with 1 method)
julia> f1()
9.999999999999999999999999999999999999999999999999999999999999999999999999859301e+02
julia> f2() = (a = d"0.0" ; for i = 1:10000 ; a += d"0.1" ; end ; a)
f2 (generic function with 1 method)
julia> f2()
+10000E-1
julia> @benchmark f1()
================ Benchmark Results ========================
Time per evaluation: 2.55 ms [139.77 μs, 4.95 ms]
Proportion of time in GC: 4.52% [0.00%, 16.41%]
Memory allocated: 1015.63 kb
Number of allocations: 30000 allocations
Number of samples: 100
Number of evaluations: 100
Time spent benchmarking: 0.31 s
julia> @benchmark f2()
================ Benchmark Results ========================
Time per evaluation: 792.35 μs [703.65 μs, 881.05 μs]
Proportion of time in GC: 0.00% [0.00%, 0.00%]
Memory allocated: 0.00 bytes
Number of allocations: 0 allocations
Number of samples: 100
Number of evaluations: 100
Time spent benchmarking: 0.10 s
julia> f1m() = (a = big"0.0" ; for i = 1:10000 ; a *= big"0.1" ; end ; a)
f1m (generic function with 1 method)
julia> f2m() = (a = d"0.0" ; for i = 1:10000 ; a *= d"0.1" ; end ; a)
f2m (generic function with 1 method)
julia> @benchmark f1m()
================ Benchmark Results ========================
Time per evaluation: 2.03 ms [0.00 ns, 4.06 ms]
Proportion of time in GC: 3.73% [0.00%, 14.73%]
Memory allocated: 1015.63 kb
Number of allocations: 30000 allocations
Number of samples: 100
Number of evaluations: 100
Time spent benchmarking: 0.23 s
julia> @benchmark f2m()
================ Benchmark Results ========================
Time per evaluation: 756.59 μs [678.94 μs, 834.24 μs]
Proportion of time in GC: 0.00% [0.00%, 0.00%]
Memory allocated: 0.00 bytes
Number of allocations: 0 allocations
Number of samples: 100
Number of evaluations: 100
Time spent benchmarking: 0.10 s
julia> for i in 1:10 @show bits(a * i) endbits(a * i) = "0011111111001001100110011001100110011001100110011001100110011010"
bits(a * i) = "0011111111011001100110011001100110011001100110011001100110011010"
bits(a * i) = "0011111111100011001100110011001100110011001100110011001100110100"
bits(a * i) = "0011111111101001100110011001100110011001100110011001100110011010"
bits(a * i) = "0011111111110000000000000000000000000000000000000000000000000000"
bits(a * i) = "0011111111110011001100110011001100110011001100110011001100110100"
bits(a * i) = "0011111111110110011001100110011001100110011001100110011001100111"
bits(a * i) = "0011111111111001100110011001100110011001100110011001100110011010"
bits(a * i) = "0011111111111100110011001100110011001100110011001100110011001101"
bits(a * i) = "0100000000000000000000000000000000000000000000000000000000000000"what I've been doing is kind of similar to tax problem (game economy simulation)for now,turning floating number to integer is enough to solve all my headaches.But I will remember your recommendation when I ran into more complicated case.Thank you
2015년 12월 28일 월요일 오후 10시 38분 21초 UTC+9, Scott Jones 님의 말: