Julian
Julian

Reputation: 1685

Can someone explain the Math.ulp(double) method?

I haven't been able to find any information online that doesn't already assume I know things. I wonder if anyone knows any good resources that I can look into to help me wrap my head around what this function does exactly?

From what I gather, and I'm pretty certain this is wrong or at least not fully right, is that given a floating point, it determines the distance between itself and some number next in a sequence? There appears to be something to do with how number are represented bitwise, but the sources I've read never explicitly said anything about that.

Upvotes: 5

Views: 7352

Answers (2)

James
James

Reputation: 319

I believe that it is showing the distance between the number you specify, and the next largest binary float that can be encoded.

Because of the range that binary floating point numbers cover and their precision, not all numbers between any two given real numbers are covered, so it looks like this is giving you the positive distance between the number you wish to encode and the actual number it would be stored as.

From Wikipedia:

the unit of least precision (ULP) is the spacing between floating-point numbers, i.e., the value the least significant digit represents if it is 1

Upvotes: 4

Bobby
Bobby

Reputation: 471

http://matlabgeeks.com/tips-tutorials/floating-point-comparisons-in-matlab/

illustrated it rather well:

float2bin(A)

//ans = 0011111110111001100110011001100110011001100110011001100110100000

float2bin(B)

//ans = 0011111110111001100110011001100110011001100110011001100110011010

You can see the difference in precision at a binary level in this example. A and B differ by 6 ulps (units in the last place)

Upvotes: 8

Related Questions