473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Finding next largest/smallest floating point value

I have a floating point number. I'd like to get the nearest floating
point number that is larger or smaller than the given number. I
investigated FLT_EPSILON but it only seems to be useful if the given
number is 1. Any suggestions?

Thanks,
-Peter
Nov 14 '05 #1
13 5102
Peter Ammon wrote:
I have a floating point number. I'd like to get the nearest floating
point number that is larger or smaller than the given number. I
investigated FLT_EPSILON but it only seems to be useful if the given
number is 1. Any suggestions?


You have to scale epsilon to find the number that results when
you change the least significant bit for any given floating point
number. This is quite complicated IIRC, especially when the
exponent changes.

The following link might cover it. I don't know, haven't read it
yet, but it seems to be very good so I am including it even if#
it is not relevant to your question.

http://docs.sun.com/source/806-3568/ncg_goldberg.html

--
Thomas.

Nov 14 '05 #2
I'm sure there's a better way, but:

float num, nextnum,guess;

// can't just add 1.0, because 1.0 could be less than a sig. bit
// play with + or -

guess = num+num;
do {
nextnum = guess;
guess = (num + nextnum) / 2.0;
}
while ((guess != num) && (guess != nextnum)); // could round down or up
"Peter Ammon" <pe*********@rocketmail.com> wrote in message
news:ca**********@news.apple.com...
I have a floating point number. I'd like to get the nearest floating
point number that is larger or smaller than the given number. I
investigated FLT_EPSILON but it only seems to be useful if the given
number is 1. Any suggestions?

Thanks,
-Peter


Nov 14 '05 #3
Peter Ammon wrote:

I have a floating point number. I'd like to get the nearest
floating point number that is larger or smaller than the given
number. I investigated FLT_EPSILON but it only seems to be
useful if the given number is 1. Any suggestions?


#include <float.h>

float scaledepsilon(float number)
{
float trial;

trial = number * FLT_EPSILON;
while (number != (number - trial/2.0) trial = number/2.0;
return trial;
} /* untested, but should be close */

Probably highly suspicious for very small values of number.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #4


Peter Ammon wrote:
I have a floating point number. I'd like to get the nearest floating
point number that is larger or smaller than the given number. I
investigated FLT_EPSILON but it only seems to be useful if the given
number is 1. Any suggestions?

Thanks,
-Peter


If you have a C99 compiler you can use the nextafter functions in math.h

7.12.11.3 The nextafter functions

double nextafter(double x, double y);
float nextafterf(float x, float y);
long double nextafterl(long double x, long double y);

Description
The nextafter functions determine the next representable value, in the
type of the function, after x in the direction of y, where x and y are
first converted to the type of the function. The nextafter functions
return y if x equals y. A range error may occur if the magnitude of x is
the largest finite value representable in the type and the result is
infinite or not representable in the type.

Returns
The nextafter functions return the next representable value in the
specified format after x in the direction of y.

Nov 14 '05 #5
In <Gu*********************@twister.southeast.rr.co m> Arin Chaudhuri <ar*****************@yahoo.com> writes:
Peter Ammon wrote:
I have a floating point number. I'd like to get the nearest floating
point number that is larger or smaller than the given number. I
investigated FLT_EPSILON but it only seems to be useful if the given
number is 1. Any suggestions?


If you have a C99 compiler you can use the nextafter functions in math.h


Nope, you need an implementation of the C99 standard library for that,
not a C99 compiler.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #6
Dan Pop wrote:
Nope, you need an implementation of the C99 standard library for that,
not a C99 compiler.

Dan

Thanks for correcting me.

Nov 14 '05 #7
Peter Ammon wrote:
I have a floating point number. I'd like to get the nearest floating
point number that is larger or smaller than the given number. I
investigated FLT_EPSILON but it only seems to be useful if the given
number is 1. Any suggestions?

Thanks,
-Peter


FLT_EPSILON is the smallest value, when added to 1.0 results in a
value greater than 1.0. If you multiply a float value by 1.0 +
FLT_EPSILON I suppose you'll get its next larger value.

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #8


Joe Wright wrote:
FLT_EPSILON is the smallest value, when added to 1.0 results in a value
greater than 1.0. If you multiply a float value by 1.0 + FLT_EPSILON I
suppose you'll get its next larger value.


I guess the next representable number after x will be
x+FLT_EPSILON, if 1<=x<2,
x+2FLT_EPSILON if 2<=x<4
x+4FLT_EPSILON if 4<=x<8
etc.

The following program does not contradict my conjecture.

#include <stdio.h>
#include <math.h>
#include <float.h>

int
main(void)
{
float x=1.5f,y=100.0f;
if ( (x*(1.f+FLT_EPSILON)) == nextafterf(x,y))
{
printf("equal1\n");
}
else if( x+FLT_EPSILON == nextafterf(x,y) )
{
printf("equal2\n");
}
x=2.f;
if ( x+FLT_EPSILON == nextafterf(x,y))/*x
{
printf("equal1\n");
}
else if( x+(FLT_EPSILON+FLT_EPSILON) == nextafterf(x,y) )
{
printf("equal2\n");
}
return 0;
}

Nov 14 '05 #9


Arin Chaudhuri wrote:
I guess the next representable number after x will be
x+FLT_EPSILON, if 1<=x<2,
x+2FLT_EPSILON if 2<=x<4
x+4FLT_EPSILON if 4<=x<8
etc.


I am assuming FLT_RADIX=2, in general I guess the powers of two should
be replaced by the powers of
FLT_RADIX.

Nov 14 '05 #10
Arin Chaudhuri wrote:


Arin Chaudhuri wrote:
I guess the next representable number after x will be
x+FLT_EPSILON, if 1<=x<2,
x+2FLT_EPSILON if 2<=x<4
x+4FLT_EPSILON if 4<=x<8
etc.


I am assuming FLT_RADIX=2, in general I guess the powers of two should
be replaced by the powers of
FLT_RADIX.


And we're talking about C. Do you have anything that compiles in C?
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #11
Arin Chaudhuri wrote:


Joe Wright wrote:
FLT_EPSILON is the smallest value, when added to 1.0 results in a
value greater than 1.0. If you multiply a float value by 1.0 +
FLT_EPSILON I suppose you'll get its next larger value.

I guess the next representable number after x will be
x+FLT_EPSILON, if 1<=x<2,
x+2FLT_EPSILON if 2<=x<4
x+4FLT_EPSILON if 4<=x<8
etc.

The following program does not contradict my conjecture.

#include <stdio.h>
#include <math.h>
#include <float.h>

int
main(void)
{
float x=1.5f,y=100.0f;
if ( (x*(1.f+FLT_EPSILON)) == nextafterf(x,y))
{
printf("equal1\n");
}
else if( x+FLT_EPSILON == nextafterf(x,y) )
{
printf("equal2\n");
}
x=2.f;
if ( x+FLT_EPSILON == nextafterf(x,y))/*x
{
printf("equal1\n");
}
else if( x+(FLT_EPSILON+FLT_EPSILON) == nextafterf(x,y) )
{
printf("equal2\n");
}
return 0;
}


What is nextafterf()? I do see it declared in my math.h in a
non-posix and non-ansi context (djgpp gcc 3.0) but it doesn't seem
to be in my info system.

After removing the /*x you left around, the code did compile but
with the -Wall -ansi switches we see nextafterf not explicitly
declared and the program not 'working'.
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #12

Joe Wright wrote:
Arin Chaudhuri wrote: Joe Wright wrote: What is nextafterf()? I do see it declared in my math.h in a non-posix
and non-ansi context (djgpp gcc 3.0) but it doesn't seem to be in my
info system.

After removing the /*x you left around, the code did compile but with
the -Wall -ansi switches we see nextafterf not explicitly declared and
the program not 'working'.


I apologize for the trouble caused by the /*.
nextafterf is a part of the C99 standard library, please have a look at
section 7.12.11.3 of the standard. The prototype of nextafterf should be
defined in math.h, I have copypasted the relevant portion of the
standard at the end of the post.

I reasoned as follows (assuming the radix to be 2) :

If, b = b0.b1 b2 b3 ... bp x 2^{e}

is a positive normalized float value, (b0=1)
then the next normalized value that can be represented can be obtained
by adding
0. 0 0 0 ... 1 x 2^{e}= 1. 0 0 0 ... 2^{e-p} = 2^eFLT_EPSILON
to the above, i.e, we increment the last possible "digit".

Hence, for float values of the form, f x 2^{e} with 1<=f<2 the next
normalized number that can be represented is obtained by adding 2^e
FLT_EPSILON

This will not work when we enter the zone of denormalized numbers, (when
b0 is 0 and e is the smallest value possible).

****
7.12.11.3 The nextafter functions

Synopsis
1 #include <math.h>
double nextafter(double x, double y);
float nextafterf(float x, float y);
long double nextafterl(long double x, long double y);

Description
The nextafter functions determine the next representable value, in the
type of the function, after x in the direction of y, where x and y are
first converted to the type of the function. The nextafter functions
return y if x equals y. A range error may occur if the magnitude of x is
the largest finite value representable in the type and the result is
infinite or not representable in the type.

Returns
The nextafter functions return the next representable value in the
specified format after x in the direction of y.

Nov 14 '05 #13


Arin Chaudhuri wrote:

This will not work when we enter the zone of denormalized numbers, (when
b0 is 0 and e is the smallest value possible).


What I meant here was e takes the value FLT_MIN_EXP, and please correct
me if I am wrong, there might exist implementations which do not have
denormals.

Nov 14 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Adam H. Peterson | last post by:
Is there a standard way to find the minimum value for a data type? I'm thinking along the lines of std::numeric_limits<T>::min(). But that doesn't work for floating point types. I can't use...
9
by: Steven D'Aprano | last post by:
I'm looking for some way to get the next floating point number after any particular float. (Any mathematicians out there: I am aware that there is no "next real number". But floats are not real...
19
by: ramu | last post by:
Hi, I have, suppose 1000 numbers, in a file. I have to find out 5 largest numbers among them without sorting. Can you please give me an efficient idea to do this? My idea is to put those numbers...
25
by: Subra | last post by:
Hi, What is the best way to find the 1000 largest numbers from the file having hell lot of entries ? Can you please help me to find out the way ? Do I need to go for B+ trees ?? Please help,...
9
by: tom | last post by:
Hi! How can I determine the smallest and largest values of numeric types (for example int) possible in my system? I think there exists a function for this task but I don't know it.
13
by: arnuld | last post by:
it runs fine. any advice for the improvement: /* C++ Primer - 4/e * * exercise 7.20 * STATEMENT: * write a programme to find the factorial of an int. * use an iteratice function. * */
5
by: davenet | last post by:
Hi, I'm new to Python and working on a school assignment. I have setup a dictionary where the keys point to an object. Each object has two member variables. I need to find the smallest value...
10
by: Steven D'Aprano | last post by:
Is there a simple, elegant way in Python to get the next float from a given one? By "next float", I mean given a float x, I want the smallest float larger than x. Bonus points if I can go in...
8
crystal2005
by: crystal2005 | last post by:
I am writing a program that receive maximum of 25 line of string each has 20 characters maximum. The program will print the smallest and the largest string. However the following program gives me...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.