Thread: lowest spacing between numbers

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    13

    lowest spacing between numbers

    I was woundering if there is some kind of function that returns what is the lowest posible spacing for some variable.
    For example, in Fortran you can use function "spacing". Lets say that a variable x is a double (in fortran real :: kind(8) = x). Then spacing(x) would return 2.225073858507201E-308. If for example x was a float, than spacing(x) would give 1.17549435082228E-38. Is there some kind of equivalent to this in c++?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    #include <limits>, then use std::numeric_limits<T>::epsilon() for the type T.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    Thanks!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    But epsilon itself is only valid when added to 1.0.

    If the value stored in the double is much larger (say 1E20), then the apparent 'epsilon' you need to add to cause a change will be so much greater.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As Salem points out, the Epsilon function is the smallest number that "will make a difference" when added/subtracted from 1.0 for any (floating point) type.

    I don't think it's trivial to come up with a "spacing" function. Do you want this to be super-fast, or is it a rarely called function?

    I'm thinking that you could do something like this:
    Code:
    double spacing(double x)
    {
       double orig = x;
       double sp; 
       sp = x / (double) (1U << 31;
       while (sp != 0.0 && (x + sp != orig)) {
          sp /= 2.0;
       }
       sp *= 2.0;
       return sp;
    }
    It's probably not very inefficient, but there may be some way that we could make it better. If you have a compiler/processor that supports 64-bit integers, you can probably use a bigger shift than 31, and reduce the number of loops needed to find the number.

    An alternative solution is to use some bit manipulation of the incoming number - that's less portable but probably a fair bit faster.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Do you need it to be portable? This one works for IEEE-754 floating point numbers.
    Code:
    static_assert(sizeof(int) == sizeof(float));
    
    float f = something;
    unsigned int &fbits = reinterpret_cast<unsigned int &>(f);
    
    const unsigned int exponent_mask = 0x7F800000;
    float epsilon;
    unsigned int &ebits = reinterpret_cast<unsigned int &>(epsilon);
    ebits = (fbits & exponent_mask) | 1;
    It works the same with double and a 64-bit integer type. In this case, the mask is 0x7FF0000000000000.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    But if the LSB of the float mantissa is already 1, then or-ing with 1 won't do anything.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Eh? I'm masking out everything but the exponent. I construct the positive floating point number that has the same exponent as the source number but the smallest possible mantissa. Which is the smallest floating pointer number that has an effect when added to or subtracted from the original floating point number.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So you are
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting an int array highest to lowest
    By switchback in forum C Programming
    Replies: 3
    Last Post: 07-27-2008, 03:30 AM
  2. Help with Rational Numbers (C++)
    By cloudjc in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2008, 04:03 PM
  3. Trying to sort numbers from lowest to highest
    By jw232 in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2008, 04:03 PM
  4. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  5. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM