Python Forum
smallest float that can increment a given value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
smallest float that can increment a given value
#1
given a float value, i want to get the smallest float value that can increment the given value. below that added to the given value leaves the same given value. these would be powers of 2. i would like to know if Python has such a function and if not, what would you name such a function?
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
Not exactly, but the math module provides the function nextafter(x,y) that returns the next floating-point value after x towards y.

>>> math.nextafter(12,0) # The next float towards 0.
11.999999999999998
>>> 12-math.nextafter(12,0)
1.7763568394002505e-15
>>> _.hex()
'0x1.0000000000000p-49'
>>> 
Naming isn't really consistent.

The decimal provides context methods called next_plus, 'next_minus, and next_towards`.

gmpy2 uses the names next_above and next_below.
DeaD_EyE and Skaperen like this post
Reply
#3
what would you pick for a name? i once did this in C and used smincr() as the name. that nextafter() method does look like it can be usable as the need is often to figure the next value and when it isn't, the difference gets the increment.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
(Dec-11-2021, 06:05 PM)Skaperen Wrote: what would you pick for a name?
There is hint in his post,as he already has chosen names in gmpy2.
Also hi is the author🔨 of gmpy2.
(Dec-11-2021, 05:32 AM)casevh Wrote: gmpy2 uses the names next_above and next_below
Skaperen likes this post
Reply
#5
so, maybe he can tell me where to get MPFR for Linux, Xubuntu 18.04.6 LTS so i can install GMPY2. maybe MPC is also needed.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
sudo apt install libmpc-dev
That will install the GMP, MPFR, and MPC libraries. You can then compile from source.

Or if you are using a version of Python distributed by Ubuntu, you can use

sudo apt install python-gmpy2
or
sudo apt install python3-gmpy2
Lastly, binary wheels are available. As of right now, the version on PyPi is rc2 (the final release candidate). The final version should be in the next day or two. Since PyPi doesn't install pre-release versions by default, you need to use this command

python3 -m pip install --pre gmpy2
where python3 should be replaced by the command that invokes the Python interpreter you are using.

I'm most interested verifying that the last option works.
Reply
#7
(Dec-11-2021, 06:35 PM)snippsat Wrote:
(Dec-11-2021, 06:05 PM)Skaperen Wrote: what would you pick for a name?
There is hint in his post,as he already has chosen names in gmpy2.
Also hi is the author🔨 of gmpy2.
(Dec-11-2021, 05:32 AM)casevh Wrote: gmpy2 uses the names next_above and next_below
I just used the same names as chosen by the MPFR library.

If I were to name a function that returns the increment (instead of functions that calculate the next, I would probably define it as float_increment(x, n) where x is the float and n is an integer specifying the direction (positive implies towards +Infinity and negative implies to -Infinity) and abs(n) specifies the number of steps to take.
Reply
#8
these days i try to install Python stuff with pip first. Ubuntu stuff is old due me running an old version of the system (bionic, 18.04.6). i'll try those installs tomorrow. been up late watching tornado news. i have relatives in one of those towns. everything is out there but they are OK. no power, no water, no gas, no phones, no cable, no internet but they have a fireplace and plenty of wood and very little damage. communication is by ham radio. so i need my sleep.
Tradition is peer pressure from dead people

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


Forum Jump:

User Panel Messages

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