Debug.Log(2000.0f * 000.1f); is 200

Now that you have had your coffee, don’t forget about floating point imprecision:

// @kurtdekker: don't forget about floating point imprecision
using UnityEngine;
public class FloatingPointImprecision : MonoBehaviour
{
    void Start ()
    {
        float a = 2000.0f;
        float b = 0.1f;
        float product = (a * b);
        float answer = 200;

        Debug.Log( "a = " + a);
        Debug.Log( "b = " + b);
        Debug.Log( "product of a*b = " + product);
        Debug.Log( "answer = " + answer);
        Debug.Log( "does product equal answer? " + (product == answer));
        Debug.Log( "does a*b equal answer? " + ((a * b) == answer));
    }
}

7399994--904268--floating_point_imprecision.png

Your mileage almost certainly will vary. Choose an epsilon term and know it well.

1 Like
How to get rotation of a Game Object??
Rotating Specific Amount In The Correct Direction on Key Press
I Have A Rounding Problem That I Can't Seem To Fix
Why give me contactfilter a wrong "false"?
My ship wont move on
How to create a time slow down effect for everything but my player ?
playerprefs compare values
Getting The Element Int OF An Array
Floats Not Working
Objects on the phone are sometimes on a totally wrong place
Uniform RigidBody acceleration->deceleration not ending up at exact target location
Rotate the camera smoothly around an object.
Problem with transform.position
Float used in expression works perfectly, but if I save the float as a variable it will not
check if line hits start and end
[SOLVED] : Explicit match on x and y velocities of a rigid body and jump function
Physics2D.Raycast distance
Character moves without pressing any buttons.
localPosition float comparison fails for seemingly no reason
Check if x position is moving
Animator Parameter can't be set to zero through code
2D Astronomical Simulation
Same color is not same color on spriterenderer?
Unity strange values when rotating an object (9.659347e-06 and 90.00001)
Recreatable incorrect raycast miss on capsule collider
Inconsistent procedural terrain
Been coding for a bit, self taught, and this question has been nagging for a while and finally decided to ask. Why is "Mathf" so poor out of the box?
Can't access transform rotation from script
Lerp not going into negative field
Vector3.MoveTowards move object with different speed on different frame rate