[julia-users] strange behavior with float value multiplication

2015-12-28 Thread Yonghee Kim
I wrote simple code like this

--
a = 0.2

for i in 1:10
  println(a * i)
end
---


and what I got is not 0.2, 0.4, 0.6, 0.8, 1.0  

but this.

0.2
0.4
0.6001
0.8
1.0
1.2002
1.4001
1.6
1.8
2.0



println(0.2 * 3) does the same thing. not 0.6 but 0.6001

does anyone know why this happens? 



Re: [julia-users] strange behavior with float value multiplication

2015-12-28 Thread Pranit Bauva
Dear Yonghee Kim,

Floating point calculations are not exact because the decimal point is
represented by only a fixed number of binary digits. The more is the
bits allocated more will be the precision. This explanation would be
clearer if you see how to convert floating point integers to binary.
http://stackoverflow.com/questions/3954498/how-to-convert-float-number-to-binary

The series will be more and more accurate if more number of terms are
added and will each added term it error will decrease by 2^-(no of
terms). Furthermore this is the problem with almost every programming
language.

Regards,
Pranit Bauva

On Mon, Dec 28, 2015 at 3:29 PM, Yonghee Kim  wrote:
> I wrote simple code like this
>
> --
> a = 0.2
>
> for i in 1:10
>   println(a * i)
> end
> ---
>
>
> and what I got is not 0.2, 0.4, 0.6, 0.8, 1.0 
>
> but this.
>
> 0.2
> 0.4
> 0.6001
> 0.8
> 1.0
> 1.2002
> 1.4001
> 1.6
> 1.8
> 2.0
>
>
>
> println(0.2 * 3) does the same thing. not 0.6 but 0.6001
>
> does anyone know why this happens?


Re: [julia-users] strange behavior with float value multiplication

2015-12-28 Thread Yonghee Kim
Dear Pranit Bauva 

Thanks for your kind explanation!
your answer helped me a lot.