Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

issues with expr

Status
Not open for further replies.

vsdpsingh

Programmer
Mar 8, 2011
32
IN
Hi,

tclsh
% expr 0.2+0.2
0.4
% expr 0.2+0.1
0.30000000000000004
%

Can anyone please explain me the logic, that if i add 0.2 with 0.2, it will give 0.4
but, if i add 0.2 with 0.1, it will give 0.30000000000000004 and not 0.3


Am i missing some thing.
thanks in advance.

 
It's because of floating point arithmetic
Use format statement
Code:
% format "%5.3f" [expr 0.2+0.2]
0.400
 
Hi

That is because the internal representation of floating point values used by computers. That behavior can be observed in many other languages :
Code:
master # cat precision.java
public class precision {
  public static void main(String[] a)
  {
    System.out.println(0.2+0.2);
    System.out.println(0.2+0.1);
  }
}

master # javac precision.java

master # java precision
0.4
0.30000000000000004
Code:
>>> 0.2+0.2
0.4
>>> 0.2+0.1
0.30000000000000004
Code:
>>> 0.2+0.2
0.4
>>> 0.2+0.1
0.30000000000000004
Code:
irb(main):001:0> 0.2+0.2
=> 0.4
irb(main):002:0> 0.2+0.1
=> 0.30000000000000004

The difference is mostly only in whether the language designers decided to beautify the output of floating point values, or not. But even if the output is beautified, behind the scenes in most cases the same internal representation is used, so you must be aware of it when comparing floating point values :
Code:
 > =0.2+0.2
0.4
> =0.2+0.1
0.3
> =0.2+0.2==0.4
true
> =0.2+0.1==0.3
false
Code:
  DB<1> print 0.2+0.2
0.4
  DB<2> print 0.2+0.1
0.3
  DB<3> print 0.2+0.2==0.4 ? 'equal' : 'not'
equal
  DB<4> print 0.2+0.1==0.3 ? 'equal' : 'not'
not


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top