Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
floating point addition
#1
given a floating point value, what is the smallest floating point value that when arithmetically added to it, does increase that given value (e.g. a smaller value added to it is too small to increment it given the working precision). i need a function that comes up with that smallest adder.

edit:

i believe i have such a function in C. but i suspect it makes use of some C library stuff. i don't how well that would translate to Python. i hope that == for comparing float to float always compares for exact equal. then i can write my own if there isn't one readily available.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
In general, sys.float_info[-3] is epsilon value for floating point numbers representation in memory. That means the next floating
point number (correctly represented in memory for a given precision,e.g. double) greater 1.0 is 1.0 + epsilon. Roughly, for a given number x,
the next floating point number would be x+x*epsilon.

(Jun-28-2019, 07:34 PM)Skaperen Wrote: i hope that == for comparing float to float always compares for exact equal.
That is not always true, e.g. 1.1 / 3.3 == 1.0 / 3.0 returns False, that is why numpy package has utility function numpy.isclose
to compare floating point numbers.
Reply
#3
(Jun-29-2019, 08:00 AM)scidam Wrote: That is not always true, e.g. 1.1 / 3.3 == 1.0 / 3.0 returns False,

But in that case, == is comparing as exactly equal. The issue is /, which is making 1.1 / 3.3 not exactly the same as 1.0 / 3.0, even though it should be mathematically.

Output:
>>> 1.0/3.0 0.3333333333333333 >>> 1.1/3.3 0.33333333333333337
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Jun-29-2019, 08:00 AM)scidam Wrote:
(Jun-28-2019, 07:34 PM)Skaperen Wrote: i hope that == for comparing float to float always compares for exact equal.
That is not always true, e.g. 1.1 / 3.3 == 1.0 / 3.0 returns False, that is why numpy package has utility function numpy.isclose
to compare floating point numbers.

there is, indeed, good value in having an isclose() method. but this is not what i mean for comparing two float values with ==. i am hoping that == is not trying to do an isclose(). if it did, that would break a lot of things.

Output:
>>> (1.1/3.3)*(2**54) 6004799503160662.0 >>> (1.0/3.0)*(2**54) 6004799503160661.0
1.1/3.3 and 1.0/3.0 do give different values. the hard float division does that. i would hope that == can tell them apart. if not == then at least something. i need that kind of precise comparison.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
i posted the solution over here.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  quad floating point Skaperen 6 4,838 Feb-25-2020, 08:16 AM
Last Post: Gribouillis
  Floating and decimal Sandeep2000 2 2,440 Aug-16-2019, 04:25 PM
Last Post: Nitram

Forum Jump:

User Panel Messages

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