lowest spacing between numbers

This is a discussion on lowest spacing between numbers within the C++ Programming forums, part of the General Programming Boards category; I was woundering if there is some kind of function that returns what is the lowest posible spacing for some ...

  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
    21,739
    #include <limits>, then use std::numeric_limits<T>::epsilon() for the type T.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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,893
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 03: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, 10:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21