Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stuck comparing two floats
#1
Good morning all,

I am trying to compare two floats, they are in fact application version numbers.
The code is old and in python 2.7 because certain modules have not been ported over to 3 yet.

The dilemma I have having is that Python says 1.9 is greater than 1.10
I can only assume it is reading it as 1.9 vs 1.1 as as opposed as 1.10.

I have tried them as strings, ints and floats but can't seem to get it to give the correct answer of what is larger.

Is there a way round this?
Reply
#2
1.9 is actually greater than 1.10 or 1.1. It doesn't matter. You are mistaken here maybe you want to compare 1.09 and 1.1
Reply
#3
The people who control the versioning of the application think of 1.9 as 1.09 which is the frustrating thing.
So I guess I have no choice either to tell them to revise their versioning logic?

Although I had a thought to convert the numbers into a string, a, b = '1.9', '1.10'[1:].strip('.') and then do an if statement with them as ints?
if int(a) > int(b):
Reply
#4
If you want to compare version strings, you've often the major.minor.micro pattern.

import sys


print(sys.version_info)
Output:
sys.version_info(major=3, minor=8, micro=3, releaselevel='final', serial=0)
The python version does have releaselevel and serial as additional information.

import sys


print(sys.version)
Output:
3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)]
If you want to use this string for comparison, you've to split version from rest and then convert each number to an int.

import sys


version_str, rest = sys.version.split(maxsplit=1)
print(version_str)
Output:
3.8.3
Now splitting by dot and converting the parts into int.
version_tuple = tuple(map(int, version_str.split(".")))
Now if you have many version infos, here some examples:
versions = [
    (3, 8, 3),
    (3, 7, 0),
    (3, 9, 0),
    (2, 7, 0),
    (3, 6, 2),
]

print(sorted(versions))
Output:
[(2, 7, 0), (3, 6, 2), (3, 7, 0), (3, 8, 3), (3, 9, 0)]
Comparing tuples or lists with more than one element, will compare the first element, then the next element and so on.
All elements inside the tuple/list must have the same data type. In this case all are integers.

If you want to compare floats on equality, use math.isclose(value_a, value_b).
But this is not to compare version strings. Just for the future.

import math


a = 0.1
b = 0.2
c = 0.3

# wrong!
if a + b == c:
    print("a == b")
else:
    print("a != b")

# right
if math.isclose(a + b, c):
    print("a == b")
else:
    print("a != b")
Further information about floating-point arithmetic: 0.30000000000000004.com
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Go get the packaging module. (It should work on 2.7 as well).

from packaging import version
print(version.parse("1.9") < version.parse("1.10"))
Output:
True
Reply
#6
It's a nice to have, but it's an external dependency. Seeking packages for everything you want to do, ends in a never-ending story. It's good to know how to do this with plain Python. Funny fact, one guy posted code on stackoverflow which is the same like mine to create the version tuple. So the way to solve this task is very straight forward.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#7
If it's a version number, you shouldn't be treating it as a float anyway!
Reply
#8
I ended up with a solution not requiring a module but that module is nice to know exists, thanks.

Basically used .split('.')[0] and [1] to compare the left and right sides then ran the if statement with int(l_num) > int(old_l_num)
and elif right num etc.

Thanks for the prodding in right direction.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  When is it safe to compare (==) two floats? Radical 4 694 Nov-12-2023, 11:53 AM
Last Post: PyDan
  floats 2 decimals rwahdan 3 1,617 Dec-19-2021, 10:30 PM
Last Post: snippsat
  rounding and floats Than999 2 3,082 Oct-26-2020, 09:36 PM
Last Post: deanhystad
  int, floats, eval menator01 2 2,428 Jun-26-2020, 09:03 PM
Last Post: menator01
  rounding floats to a number of bits Skaperen 2 2,307 Sep-13-2019, 04:37 AM
Last Post: Skaperen
  comparing fractional parts of floats Skaperen 4 3,348 Mar-19-2019, 03:19 AM
Last Post: casevh
  Integer vs Floats Tazbo 2 2,868 Jan-09-2019, 12:06 PM
Last Post: Gribouillis
  Formatting floats Irhcsa 6 4,138 Oct-04-2018, 04:23 PM
Last Post: volcano63
  How do you sort a table by one column?? (of floats) sortedfunctionfails 3 12,271 Jan-11-2018, 09:04 AM
Last Post: sortedfunctionfails
  Reading floats and ints from csv-like file Krookroo 15 20,046 Sep-05-2017, 03:58 PM
Last Post: Krookroo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020