(1 > 1) is true

This is the place for queries that don't fit in any of the other categories.

(1 > 1) is true

Postby valtih » Fri Apr 04, 2014 11:14 am

code

Code: Select all
if score > best:
   print str(score) + " > " + str(best)

result

Code: Select all
43.4925092553 > 43.4925092553
43.4925092553 > 43.4925092553

Do you see the problem?
Last edited by metulburr on Fri Apr 04, 2014 11:26 am, edited 1 time in total.
Reason: locked
valtih
 
Posts: 4
Joined: Fri Apr 04, 2014 11:09 am

Re: (1 > 1) is true

Postby metulburr » Fri Apr 04, 2014 11:29 am

What python version are you running?
What operating system?
Is this the only code influencing the output?
What is the initial value of score and best?
Make a runable code snippet.

Regardless, something you are doing is incorrect:
Code: Select all
>>> if 1 > 1:
...     print('not going to print')
...
>>> if 43.4925092553 > 43.4925092553:
...     print('not going to print either')
...
>>>
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: (1 > 1) is true

Postby valtih » Fri Apr 04, 2014 11:45 am

Code: Select all
best = 43.4925092553
score = math.exp(1)*16
if score > best:
   print str(score) + " > " + str(best)
valtih
 
Posts: 4
Joined: Fri Apr 04, 2014 11:09 am

Re: (1 > 1) is true

Postby metulburr » Fri Apr 04, 2014 12:21 pm

that is because score is greater than best:

Code: Select all
metulburr@rch ~ $ python2
Python 2.7.6 (default, Feb 26 2014, 12:07:17)
[GCC 4.8.2 20140206 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> a = math.exp(1)*16
>>> a
43.49250925534472
>>> b = 43.4925092553
>>> a > b
True
>>>
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: (1 > 1) is true

Postby Mekire » Fri Apr 04, 2014 12:22 pm

e times 16 is indeed greater than the number you are comparing it to.
Code: Select all
>>> math.e
2.718281828459045
>>> math.e*16
43.49250925534472
>>> (math.e*16)>43.4925092553
True
>>>

You will need some sort of tolerance if you want to compare floats to this precision.

Something like:
Code: Select all
>>> tolerance = 1e-9
>>> math.e*16 > 43.4925092553
True
>>> math.e*16-43.4925092553 > tolerance
False
>>> math.e*16-43.492509254 > tolerance
True

-Mek

Edit: You can also use the round() builtin, but floating point precision errors still apply.
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: (1 > 1) is true

Postby valtih » Fri Apr 04, 2014 12:50 pm

Why doesn't str(value) does not report the difference?
valtih
 
Posts: 4
Joined: Fri Apr 04, 2014 11:09 am

Re: (1 > 1) is true

Postby Mekire » Fri Apr 04, 2014 1:04 pm

print only shows a set number of significant digits (which apparently differ between python2 and 3). You can ask for as many digits as you want with string formatting.

Code: Select all
>>> print("{:.50}".format(math.e))
2.7182818284590450907955982984276488423347473144531
The string interpretation doesn't change the actual value though.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], snippsat and 4 guests