Notices
Results 1 to 7 of 7

Thread: The smallest floating point x such that x+2=x

  1. #1 The smallest floating point x such that x+2=x 
    New Member
    Join Date
    Nov 2011
    Posts
    3
    Hi! I want to calculate the smallest floating point x such that x+2=x. I've written a programme in C that does not actually determines the smallest floating point x such that x+2=x, but rather determines a power of two within an interval that contains the smallest floating point x. Does anyone have any ideas how can I improve this solution or find out a new one? Is there any built-in function in MATLAB and/or C to get accurate approximations of this x?

    Code:
    #include <stdio.h>
     
     int main( int argc, char **argv )
     {
        float x = 1.0f;
     
        printf( "current x \t 2 + current x\n" );
        do {
           printf( "%G\t\t %.20f\n", x, (2.0f + x) );
           x *= 2.0f;
        }
        while ((float)(2.0 + x) != x);
     
        printf( "\nCalculated x: %G\n", x );
        return 0;
     }
    Code:
    Calculated x: 3.68935E+019


    Last edited by phil.st; November 10th, 2011 at 09:31 AM.
    Reply With Quote  
     

  2.  
     

  3. #2 binary search 
    Forum Freshman
    Join Date
    Oct 2010
    Posts
    98
    What about binary search? Run it just after your do - while loop. You'll have
    Code:
      double hi = 2.0 * x;
      double lo = x;
    
      while ( lo != hi ) // maybe lo < hi
      {
        float mi = (lo + hi) * 0.5;
    
        if ( (float)(mi + 2.0) == 2.0 )
        {
          lo = mi;
        }
        else
        {
          hi = mi;
        }
      }
    Or something like that.


    Reply With Quote  
     

  4. #3  
    New Member
    Join Date
    Nov 2011
    Posts
    3
    @BlasW
    Calculated hi, lo and mi: 3.68935E+019
    Any ideas?
    Reply With Quote  
     

  5. #4  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    17,036
    This is (obviously?) dependent on the implementation of floating point numbers. Wouldn't it be easier to just look at the encoding (presumably for 64-bit IEEE)? You should be able to work it out from this: Double precision floating-point format - Wikipedia, the free encyclopedia

    It looks like the answer should be 3.6028797 1016 (which is pretty close to your result).

    And in MATLAB, isn't this 2 x bitmax (http://www.mathworks.de/help/techdoc/ref/bitmax.html)

    EDIT: not sure that is correc ... but I don't have time to work out why!
    Reply With Quote  
     

  6. #5  
    New Member
    Join Date
    Nov 2011
    Posts
    3
    Quote Originally Posted by Strange View Post
    This is (obviously?) dependent on the implementation of floating point numbers. Wouldn't it be easier to just look at the encoding (presumably for 64-bit IEEE)? You should be able to work it out from this: Double precision floating-point format - Wikipedia, the free encyclopedia

    It looks like the answer should be 3.6028797 1016 (which is pretty close to your result).

    And in MATLAB, isn't this 2 x bitmax (Maximum double-precision floating-point integer - MATLAB)

    EDIT: not sure that is correc ... but I don't have time to work out why!
    Sorry! I read the articles but I don't understand at all. How did you calculate the x? Could you be more specific?
    Reply With Quote  
     

  7. #6  
    Brassica oleracea Strange's Avatar
    Join Date
    Oct 2011
    Location
    喫茶店
    Posts
    17,036
    My answer was wrong because I thought you were dealing with doubles not floats. But, from that wikipedia page: "from 253 to 254 ... the representable numbers are the even ones". So you could add 2 to any number in this range and you would get the next number. So (I think) the smallest number where adding 2 would have no effect is 2^54 = 3.6028797E16.

    But as you are looking at floats, I would have expected the number to be 2^26 = about 6.7E7. (But I am not convinced!)

    Possible problems with your code: in your loop test, you add 2.0 (a double) instead of 2.0f, this will promote both values before doing the additions so you will get all sorts of complications due to rounding; you do an equality test which is never a good idea with floating point numbers.
    Reply With Quote  
     

  8. #7  
    Forum Bachelors Degree martillo's Avatar
    Join Date
    Apr 2005
    Location
    Uruguay
    Posts
    463
    The equation has no solution. Each term on the both sides of the equal signus represent two parallel straight lines separated by 2 units which never intersect.
    In other words, x+2=x is the same as x+2-x=0 what is the same as 2=0 which is an absurd meaning no solution.
    Reply With Quote  
     

Similar Threads

  1. Floating point representation
    By Swedgen in forum Mathematics
    Replies: 4
    Last Post: March 25th, 2011, 04:02 AM
  2. The smallest path:--The NANOPATH
    By sandeep anand in forum Personal Theories & Alternative Ideas
    Replies: 8
    Last Post: October 31st, 2010, 12:06 AM
  3. Replies: 1
    Last Post: May 20th, 2010, 07:24 PM
  4. Algorithm for 2nd smallest value w/ n + ceiling(lg n) - 2
    By fwonger in forum Computer Science
    Replies: 0
    Last Post: September 24th, 2008, 05:34 PM
  5. Smallest thing viewed
    By JasonRShane in forum Chemistry
    Replies: 16
    Last Post: June 24th, 2007, 09:33 AM
Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •