Expecting calculation to be 0

This topic is 2392 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

If I pass in amount=25 rotation=90 to the method below, the result is a weird -1.5308086E-15 instead of (what I thought) should be 0. Why is this?

/** * @param amount The distance to move. Will always be positive. * @param rotation * @return The distance resulting from moving <code>amount</code> in the direction of <code>rotation</code>. */ public static float getYDistanceMovedBy(float amount, float rotation) { return Math.abs(amount) * (float) -Math.cos(Math.toRadians(rotation)); }

public void testGetYDistanceMovedBy2() { float amount = 25.0f; float rotation = 90.0f; float distance = Geometry.getYDistanceMovedBy(amount, rotation); assertEquals(0.0f, distance); }

The same code for the x axis works as expected, with the result being 0:
/** * @param amount The distance to move. Will always be positive. * @param rotation * @return The distance resulting from moving <code>amount</code> in the direction of <code>rotation</code>. */ public static float getXDistanceMovedBy(float amount, float rotation) { return Math.abs(amount) * (float) Math.sin(Math.toRadians(rotation)); }

public void testGetXDistanceMovedBy2() { float amount = 25.0f; float rotation = 0.0f; float distance = Geometry.getXDistanceMovedBy(amount, rotation); assertEquals(0.0f, distance); }

Cheers.

Share on other sites

Without looking over much of your code, it's because floats can easily have rounding errors, you will notice your incorrect value is really close to 0 anyway (Hopefully close enough):

http://www.cprogramm...ting_point.html

Yeah I thought it might be something to do with that, but I don't know how to account for it without affecting legitimate values.

Share on other sites
It is also worth noting that the vast majority of the time, you can get away with doing only "less than" or "more than" comparisons, in such a way that in borderline cases it doesn't matter much whether those test as true or false. Algorithms with this feature tend to be robust.

Share on other sites
Hidden
I'm roughly familiar with epsilon comparison, but I was under the impression that it was a far smaller threshold than -1.5 (the value in question)? I'm also a bit confused as to how comparison relates to my problem when there are none taking place. If it was a smaller number (ie < 1), I'd understand...

If I pass in amount=25 rotation=90 to the method below, the result is a weird -1.5308086E-15 instead of (what I thought) should be 0. Why is this?
...

I'm roughly familiar with epsilon comparison, but I was under the impression that it was a far smaller threshold than -1.5 (the value in question)?

The value in question seems me -1.5*10[sup]-15[/sup] (notice the E-15 in -1.5308086E-15, see e.g. this in wikipedia), what is -0.0000000000000015 (if I've counted correctly).

Share on other sites

I'm roughly familiar with epsilon comparison, but I was under the impression that it was a far smaller threshold than -1.5 (the value in question)? I'm also a bit confused as to how comparison relates to my problem when there are none taking place. If it was a smaller number (ie < 1), I'd understand...

The value [color=#1C2837][size=2]-1.5308086E-15 actually represents something like -0.0000000000000153 - which is rather close to 0.

[color="#1C2837"]

You can think of the "E-15" as a shift of the decimal point. Its a short hand notation for extremely large or small numbers.

[color="#1C2837"]

The previous posters have linked some nice resources, especially rip-off's post.

Share on other sites

[quote name='Mybowlcut' timestamp='1320564695' post='4881003']
If I pass in amount=25 rotation=90 to the method below, the result is a weird -1.5308086E-15 instead of (what I thought) should be 0. Why is this?
...

I'm roughly familiar with epsilon comparison, but I was under the impression that it was a far smaller threshold than -1.5 (the value in question)?

The value in question seems me -1.5*10[sup]-15[/sup], what is -0.0000000000000015 (if I've counted correctly). That is somewhat close to 0, and definitely less than 1 ;)
[/quote]
Why is it that the one time someone responds quickly on GameDev, I make a really stupid post? I tried deleting it, but you're just too quick. Haha.

Ok, so basically clamp to 0 if it's an acceptable margin of error?

• 10
• 16
• 9
• 13
• 41