What's wrong with float numbers?

A forum for general discussion of the Python programming language.

What's wrong with float numbers?

Postby b849961@drdrb.com » Sun Nov 03, 2013 2:27 am

I'm using Python 3.2.3. I can't find difference with float numbers. There have code and screen shot of error about that. Please help me...

Code: Select all
Difference = 0
A = float(input ('Input Number 1 :- '))
B = float(input ('Input Number 2 :- '))
Difference = A-B
print ('Difference is',Difference)


Untitled.jpg
Untitled.jpg (132.54 KiB) Viewed 253 times
My Python Version is 3.2.3
b849961@drdrb.com
 
Posts: 13
Joined: Thu Oct 24, 2013 12:41 pm

Re: What's wrong with float numbers?

Postby Mekire » Sun Nov 03, 2013 3:12 am

The problem is that base 2 can't represent base 10 fractions particularly well.

Read this:
http://docs.python.org/2/tutorial/floatingpoint.html

-Mek

Edit:
This is probably what you want:
Code: Select all
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
difference = a-b
print ("Difference is: {:.2f}".format(difference))
Code: Select all
>>>
Enter first number: 25.25
Enter second number: 45.55
Difference is: -20.30
>>>
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: What's wrong with float numbers?

Postby b849961@drdrb.com » Sun Nov 03, 2013 4:01 am

Mekire wrote:The problem is that base 2 can't represent base 10 fractions particularly well.

Read this:
http://docs.python.org/2/tutorial/floatingpoint.html

-Mek

Edit:
This is probably what you want:
Code: Select all
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
difference = a-b
print ("Difference is: {:.2f}".format(difference))
Code: Select all
>>>
Enter first number: 25.25
Enter second number: 45.55
Difference is: -20.30
>>>


Thank you Mekire. I solved that problem with your help. Thank you Sooo Much...
My Python Version is 3.2.3
b849961@drdrb.com
 
Posts: 13
Joined: Thu Oct 24, 2013 12:41 pm


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 1 guest