×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• 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.

# 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.

### 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)

#### 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.

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:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!