INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

issues with expr

issues with expr

(OP)
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.

RE: issues with expr

It's because of floating point arithmetic
Use format statement

CODE

% format "%5.3f" [expr 0.2+0.2]
0.400 

RE: issues with expr

Hi

That is because the internal representation of floating point values used by computers. That behavior can be observed in many other languages :

CODE --> Java - command line

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 --> JavaScript - FireBug Console

>>> 0.2+0.2
0.4
>>> 0.2+0.1
0.30000000000000004 

CODE --> Python - python

>>> 0.2+0.2
0.4
>>> 0.2+0.1
0.30000000000000004 

CODE --> Ruby - irb

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 --> Lua - lua

> =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 --> Perl - perl -de 42

  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

RE: issues with expr

(OP)
Thanks for reply. I will add check for the same. smile

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close