473,396 Members | 2,002 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,396 software developers and data experts.

Distance between doubles, etc

Hello!

Suppose x is a double not equal to Inf or NaN. How can I find the
smallest double y, such that x<y? It would be nice not having to deal
with bits, big and little endian and so forth. (But maybe I have to.)

Is there a quick and dirty way to get a rough estimate? Is the number
DBL_EPSILON*x of any value?

Dec 14 '06 #1
2 1194
ja****@gmail.com wrote:
>
Suppose x is a double not equal to Inf or NaN. How can I find the
smallest double y, such that x<y? It would be nice not having to deal
with bits, big and little endian and so forth. (But maybe I have to.)
nextafter(x, numeric_limits<double>::max())

nextafter was added to C in C99, and is in C++'s TR1. It's also been
voted into the next version of the standard.

If your implementation doesn't supply it you'll probably have to do some
bit twiddling.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Dec 14 '06 #2
Hello,
Suppose x is a double not equal to Inf or NaN. How can I find the
smallest double y, such that x<y? It would be nice not having to deal
with bits, big and little endian and so forth. (But maybe I have to.)

nextafter(x, numeric_limits<double>::max())

nextafter was added to C in C99, and is in C++'s TR1. It's also been
voted into the next version of the standard.

If your implementation doesn't supply it you'll probably have to do some
bit twiddling.
A portable /nextafter(x,numeric_limits<double>::max())/ could look like
the one given after the signature. Without any explicit or implied
warranty ;-)

Cheers,
Loic.
--
#include <float.h>

/*
* return the smallest double y, such that x < y
*/
double
my_nextafter(double x)
{
double y;
if (x>=0) {
y = x*(1+DBL_EPSILON);
} else {
y = x*(1-DBL_EPSILON);
}
double middle;
while (x < y ) {
middle = (x+y)/2;
if (x < middle && y!=middle) {
y = middle;
} else {
break;
}
}
return y;
}

Dec 14 '06 #3

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

Similar topics

20
by: Xenophobe | last post by:
I have successfully converted the ASP code included in the following article to PHP: http://www.4guysfromrolla.com/webtech/040100-1.shtml As described the high and low latitudes and longitudes...
4
by: Xenophobe | last post by:
I have successfully created a zip code radius search, but the performance is unacceptable. I have two tables. The first is 52K zip codes w/lat and long. The second is 3K national business...
4
by: DellaCroce | last post by:
Does anyone here have the formula for calculating distance give two pairs of Longitude/Latitude coordinates? Please share this with me if you would. -- Greg
10
by: Alan Johnson | last post by:
24.1.1.3 says about InputIterators: Algorithms on input iterators should never attempt to pass through the same iterator twice. They should be single pass algorithms. In 24.3.4.4 summarizes the...
9
by: nottheartistinquestion | last post by:
As an intellectual exercise, I've implemented an STL-esque List<and List<>::Iterator. Now, I would like a signed distance between two iterators which corresponds to their relative position in the...
4
by: cmrhema | last post by:
Hello I am having a table where the speed of the vehicle at the respective time are stored. Now I should calculate distance the eg details of vehicle No(VNo), speed,datetime is given below vno ...
18
by: lovecreatesbea... | last post by:
1. The following code snippet uses minus operation on two pointers to calculate the distance between struct members. This is illegal, right? 2. s1 and s2 are type of the same struct S. Can the...
1
by: tiffrobe | last post by:
I'm a little lost on my program. Everything works fine except function 3. It gives out garbage numbers. Its suppose to give the distance between two points and then the area of 2 circles. ...
11
by: devnew | last post by:
hello while trying to write a function that processes some numpy arrays and calculate euclidean distance ,i ended up with this code (though i used numpy ,i believe my problem has more to do with...
3
by: jcl43 | last post by:
so after that part, I have to make a function that returns a list of total distances between each of the cities in the cities list (a list of pair-lists) and I have to have it so the function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.