I'm trying to solve a problem of finding a floating point (let's say F) so small that when it adds to the user's input, the value is still equals to input. (F + input = input).

I know a float value min and max is:
1.4E-45
3.4028235E38

I tried to program it but it loops for over 30mins now LOL. I need help or at least a few hints.

blah mysterious wrote:I'm trying to solve a problem of finding a floating point (let's say F) so small that when it adds to the user's input, the value is still equals to input. (F + input = input).

How about F == 0.0F? That satisfies both your requirement and the laws of mathematics.

Other than that, use a binary search:
This will not end if X is too small though. A different way to ensure this ends may be to keep the old values of both f1 and f2 and abort if both are the same.

I've just tried the solution with keeping track of the old values (initialize to Float.NaN so initially they will certainly be different). You can then even initialize f1 to 0. The result is around 1E-7 when applied to Math.PI. If you switch to double the same code will work and you end up around 2E-16.

Josh Sakura
Greenhorn

Joined: Apr 02, 2010
Posts: 3

posted

0

Base on your first post, I got a hint of how you broke down the number between 1.0 and 1.4e-45 and see which half satisfy: F + input = input. Kind of give me an idea of bunch of nested methods:

while
if
else if
else if
else if

continuously break down which halves,quarter,etc... until I get a number that can add to input but still equals to input.

Oh I forgot to mention I must find the highest possible float value that: float + input = input.

(isn't your code restricted to input less than 1?) Sorry I'm java newbie .

I don't understand the 2nd post involving Math.PI. If you care to elaborate, it'll be much appreciated.

Josh Sakura wrote:continuously break down which halves,quarter,etc... until I get a number that can add to input but still equals to input.

With this loop you don't need to use nested if-statements. You find the position between f1 and f2, and then either shift f1 or f2. The next loop iteration will then take do the same, so it will automatically go to one quarter. The next loop iteration to eighths. The next loop iteration to sixteenths, etc.

Oh I forgot to mention I must find the highest possible float value that: float + input = input.

Have you tried it? Because that's exactly what my algorithm does. If the loop ends the last iteration did not change f1 or f2, so iterating further would cause a loop. In other words, you can't get any more specific. f1 will be the largest value for which f + input = input.

(isn't your code restricted to input less than 1?)

No. The upper limit of 1 for f2 is merely chosen because except for numbers like Float.POSITIVE_INFINITY and Float.NEGATIVE_INFINITY, input + 1 != input.

I don't understand the 2nd post involving Math.PI. If you care to elaborate, it'll be much appreciated.

I just used Math.PI (cast to float) as example input. I've also used Math.E, Float.NaN and other constants. I even tried Float.MIN_VALUE which lead to the smallest value being 0.0f.

Josh Sakura
Greenhorn

Joined: Apr 02, 2010
Posts: 3

posted

0

What is the variable X there in your code? I tried it but couldn't run because I can't figure out what X is.

You know floats in the normal range ie > (Float#MIN_NORMAL) work in 24 bits' precision? Well, you multiply that by 0.3010, ie log10(2), and you get slightly over 7.2. So that means a float can be at the very best precise to 7.225 decimal sig fig; if your values differ by >7.2 sig fig or by a ratio of >2^24, you should get no change when you try arithmetic. If you are below the Float#MIN_NORMAL value, the precision gradually reduces.

Josh Sakura wrote:What is the variable X there in your code? I tried it but couldn't run because I can't figure out what X is.

X is a constant that is the possible allowed difference. That example is not that good; the alternative with keeping track of the old values is a lot better.

Don't get me started about those stupid light bulbs.

subject: Need help on a problem to find a float point so small it doesn't change the value of input.