Handling float discrepancy in Unity3d

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
5
down vote

favorite












I know there are thousands of posts and questions around the topic of Float Discrepancy. And this is not really a question but a discussion for it.



After sitting repeatedly in front of the same problem, I made the following class to handle it once and for all of our projects. The direct value comparison is from the book The Art of Computer Programming by Donald E. Knuth. I just changed to unitys Mathf functionality.



So what is your take on the problem? Anything to add, something wrong?



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class FloatDiscrepancy
public static float Accuracy = 0.00001f;

public static float SqrAccuracy
get
return Accuracy * Accuracy;

private set


public static bool V4Equal(Vector4 a, Vector4 b)
return Vector4.SqrMagnitude(a - b) < SqrAccuracy;


public static bool V3Equal(Vector3 a, Vector3 b)
return Vector3.SqrMagnitude(a - b) < SqrAccuracy;


public static bool V2Equal (Vector2 a, Vector2 b)
return Vector2.SqrMagnitude (a - b) < SqrAccuracy;


public static bool QuaternionEqual(Quaternion a, Quaternion b)
return Quaternion.Angle (a, b) < Accuracy;


//Took the following from literature: The Art of Computer Programming by Donald E. Knuth
public static bool approximatelyEqual(float a, float b)
return Mathf.Abs(a - b) <= ( (Mathf.Abs(a) < Mathf.Abs(b) ? Mathf.Abs(b) : Mathf.Abs(a)) * Accuracy);


public static bool essentiallyEqual(float a, float b)
return Mathf.Abs(a - b) <= ( (Mathf.Abs(a) > Mathf.Abs(b) ? Mathf.Abs(b) : Mathf.Abs(a)) * Accuracy);


public static bool definitelyGreaterThan(float a, float b)
return (a - b) > ( (Mathf.Abs(a) < Mathf.Abs(b) ? Mathf.Abs(b) : Mathf.Abs(a)) * Accuracy);


public static bool definitelyLessThan(float a, float b)
return (b - a) > ( (Mathf.Abs(a) < Mathf.Abs(b) ? Mathf.Abs(b) : Mathf.Abs(a)) * Accuracy);








share|improve this question





















  • I believe this is the same implementation that Unity uses for their Vector comparisons, at least the V3Equal and V4Equal. Vector3 and Vector4 appear to have the same description. docs.unity3d.com/ScriptReference/Vector3-operator_eq.html docs.unity3d.com/ScriptReference/Vector4-operator_eq.html I'm guessing the difference lies in your bottom 4 functions though, I don't know a lot about the topic.
    – Shelby115
    Apr 5 at 14:24











  • It is in fact the same implemantation. Though you can set an accuracy value in my class. And i dont know about the quaternion one :)
    – Binary Impact BIG
    Apr 5 at 17:52
















up vote
5
down vote

favorite












I know there are thousands of posts and questions around the topic of Float Discrepancy. And this is not really a question but a discussion for it.



After sitting repeatedly in front of the same problem, I made the following class to handle it once and for all of our projects. The direct value comparison is from the book The Art of Computer Programming by Donald E. Knuth. I just changed to unitys Mathf functionality.



So what is your take on the problem? Anything to add, something wrong?



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class FloatDiscrepancy
public static float Accuracy = 0.00001f;

public static float SqrAccuracy
get
return Accuracy * Accuracy;

private set


public static bool V4Equal(Vector4 a, Vector4 b)
return Vector4.SqrMagnitude(a - b) < SqrAccuracy;


public static bool V3Equal(Vector3 a, Vector3 b)
return Vector3.SqrMagnitude(a - b) < SqrAccuracy;


public static bool V2Equal (Vector2 a, Vector2 b)
return Vector2.SqrMagnitude (a - b) < SqrAccuracy;


public static bool QuaternionEqual(Quaternion a, Quaternion b)
return Quaternion.Angle (a, b) < Accuracy;


//Took the following from literature: The Art of Computer Programming by Donald E. Knuth
public static bool approximatelyEqual(float a, float b)
return Mathf.Abs(a - b) <= ( (Mathf.Abs(a) < Mathf.Abs(b) ? Mathf.Abs(b) : Mathf.Abs(a)) * Accuracy);


public static bool essentiallyEqual(float a, float b)
return Mathf.Abs(a - b) <= ( (Mathf.Abs(a) > Mathf.Abs(b) ? Mathf.Abs(b) : Mathf.Abs(a)) * Accuracy);


public static bool definitelyGreaterThan(float a, float b)
return (a - b) > ( (Mathf.Abs(a) < Mathf.Abs(b) ? Mathf.Abs(b) : Mathf.Abs(a)) * Accuracy);


public static bool definitelyLessThan(float a, float b)
return (b - a) > ( (Mathf.Abs(a) < Mathf.Abs(b) ? Mathf.Abs(b) : Mathf.Abs(a)) * Accuracy);








share|improve this question





















  • I believe this is the same implementation that Unity uses for their Vector comparisons, at least the V3Equal and V4Equal. Vector3 and Vector4 appear to have the same description. docs.unity3d.com/ScriptReference/Vector3-operator_eq.html docs.unity3d.com/ScriptReference/Vector4-operator_eq.html I'm guessing the difference lies in your bottom 4 functions though, I don't know a lot about the topic.
    – Shelby115
    Apr 5 at 14:24











  • It is in fact the same implemantation. Though you can set an accuracy value in my class. And i dont know about the quaternion one :)
    – Binary Impact BIG
    Apr 5 at 17:52












up vote
5
down vote

favorite









up vote
5
down vote

favorite











I know there are thousands of posts and questions around the topic of Float Discrepancy. And this is not really a question but a discussion for it.



After sitting repeatedly in front of the same problem, I made the following class to handle it once and for all of our projects. The direct value comparison is from the book The Art of Computer Programming by Donald E. Knuth. I just changed to unitys Mathf functionality.



So what is your take on the problem? Anything to add, something wrong?



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class FloatDiscrepancy
public static float Accuracy = 0.00001f;

public static float SqrAccuracy
get
return Accuracy * Accuracy;

private set


public static bool V4Equal(Vector4 a, Vector4 b)
return Vector4.SqrMagnitude(a - b) < SqrAccuracy;


public static bool V3Equal(Vector3 a, Vector3 b)
return Vector3.SqrMagnitude(a - b) < SqrAccuracy;


public static bool V2Equal (Vector2 a, Vector2 b)
return Vector2.SqrMagnitude (a - b) < SqrAccuracy;


public static bool QuaternionEqual(Quaternion a, Quaternion b)
return Quaternion.Angle (a, b) < Accuracy;


//Took the following from literature: The Art of Computer Programming by Donald E. Knuth
public static bool approximatelyEqual(float a, float b)
return Mathf.Abs(a - b) <= ( (Mathf.Abs(a) < Mathf.Abs(b) ? Mathf.Abs(b) : Mathf.Abs(a)) * Accuracy);


public static bool essentiallyEqual(float a, float b)
return Mathf.Abs(a - b) <= ( (Mathf.Abs(a) > Mathf.Abs(b) ? Mathf.Abs(b) : Mathf.Abs(a)) * Accuracy);


public static bool definitelyGreaterThan(float a, float b)
return (a - b) > ( (Mathf.Abs(a) < Mathf.Abs(b) ? Mathf.Abs(b) : Mathf.Abs(a)) * Accuracy);


public static bool definitelyLessThan(float a, float b)
return (b - a) > ( (Mathf.Abs(a) < Mathf.Abs(b) ? Mathf.Abs(b) : Mathf.Abs(a)) * Accuracy);








share|improve this question













I know there are thousands of posts and questions around the topic of Float Discrepancy. And this is not really a question but a discussion for it.



After sitting repeatedly in front of the same problem, I made the following class to handle it once and for all of our projects. The direct value comparison is from the book The Art of Computer Programming by Donald E. Knuth. I just changed to unitys Mathf functionality.



So what is your take on the problem? Anything to add, something wrong?



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class FloatDiscrepancy
public static float Accuracy = 0.00001f;

public static float SqrAccuracy
get
return Accuracy * Accuracy;

private set


public static bool V4Equal(Vector4 a, Vector4 b)
return Vector4.SqrMagnitude(a - b) < SqrAccuracy;


public static bool V3Equal(Vector3 a, Vector3 b)
return Vector3.SqrMagnitude(a - b) < SqrAccuracy;


public static bool V2Equal (Vector2 a, Vector2 b)
return Vector2.SqrMagnitude (a - b) < SqrAccuracy;


public static bool QuaternionEqual(Quaternion a, Quaternion b)
return Quaternion.Angle (a, b) < Accuracy;


//Took the following from literature: The Art of Computer Programming by Donald E. Knuth
public static bool approximatelyEqual(float a, float b)
return Mathf.Abs(a - b) <= ( (Mathf.Abs(a) < Mathf.Abs(b) ? Mathf.Abs(b) : Mathf.Abs(a)) * Accuracy);


public static bool essentiallyEqual(float a, float b)
return Mathf.Abs(a - b) <= ( (Mathf.Abs(a) > Mathf.Abs(b) ? Mathf.Abs(b) : Mathf.Abs(a)) * Accuracy);


public static bool definitelyGreaterThan(float a, float b)
return (a - b) > ( (Mathf.Abs(a) < Mathf.Abs(b) ? Mathf.Abs(b) : Mathf.Abs(a)) * Accuracy);


public static bool definitelyLessThan(float a, float b)
return (b - a) > ( (Mathf.Abs(a) < Mathf.Abs(b) ? Mathf.Abs(b) : Mathf.Abs(a)) * Accuracy);










share|improve this question












share|improve this question




share|improve this question








edited Apr 5 at 12:45









t3chb0t

32k54195




32k54195









asked Apr 5 at 12:07









Binary Impact BIG

667




667











  • I believe this is the same implementation that Unity uses for their Vector comparisons, at least the V3Equal and V4Equal. Vector3 and Vector4 appear to have the same description. docs.unity3d.com/ScriptReference/Vector3-operator_eq.html docs.unity3d.com/ScriptReference/Vector4-operator_eq.html I'm guessing the difference lies in your bottom 4 functions though, I don't know a lot about the topic.
    – Shelby115
    Apr 5 at 14:24











  • It is in fact the same implemantation. Though you can set an accuracy value in my class. And i dont know about the quaternion one :)
    – Binary Impact BIG
    Apr 5 at 17:52
















  • I believe this is the same implementation that Unity uses for their Vector comparisons, at least the V3Equal and V4Equal. Vector3 and Vector4 appear to have the same description. docs.unity3d.com/ScriptReference/Vector3-operator_eq.html docs.unity3d.com/ScriptReference/Vector4-operator_eq.html I'm guessing the difference lies in your bottom 4 functions though, I don't know a lot about the topic.
    – Shelby115
    Apr 5 at 14:24











  • It is in fact the same implemantation. Though you can set an accuracy value in my class. And i dont know about the quaternion one :)
    – Binary Impact BIG
    Apr 5 at 17:52















I believe this is the same implementation that Unity uses for their Vector comparisons, at least the V3Equal and V4Equal. Vector3 and Vector4 appear to have the same description. docs.unity3d.com/ScriptReference/Vector3-operator_eq.html docs.unity3d.com/ScriptReference/Vector4-operator_eq.html I'm guessing the difference lies in your bottom 4 functions though, I don't know a lot about the topic.
– Shelby115
Apr 5 at 14:24





I believe this is the same implementation that Unity uses for their Vector comparisons, at least the V3Equal and V4Equal. Vector3 and Vector4 appear to have the same description. docs.unity3d.com/ScriptReference/Vector3-operator_eq.html docs.unity3d.com/ScriptReference/Vector4-operator_eq.html I'm guessing the difference lies in your bottom 4 functions though, I don't know a lot about the topic.
– Shelby115
Apr 5 at 14:24













It is in fact the same implemantation. Though you can set an accuracy value in my class. And i dont know about the quaternion one :)
– Binary Impact BIG
Apr 5 at 17:52




It is in fact the same implemantation. Though you can set an accuracy value in my class. And i dont know about the quaternion one :)
– Binary Impact BIG
Apr 5 at 17:52










1 Answer
1






active

oldest

votes

















up vote
2
down vote













I can't quite figure out in which situation you would use approximatelyEqual() and in which essentiallyEqual(). and what the difference really should be.



I made some tests and overall it seems to work. I did though find the below strange behavior:



Testing with these two values:



 float a = 1.0f; 
float b = 1.0f + 1e-6f;

Console.WriteLine($"a: a:F15");
Console.WriteLine($"b: b:F15");
Console.WriteLine($"a - b: a - b:F15");
Console.WriteLine($"b - a: b - a:F15");
Console.WriteLine();

Console.WriteLine($"approximatelyEqual: approximatelyEqual(a, b)");
Console.WriteLine($"essentiallyEqual: essentiallyEqual(a, b)");
Console.WriteLine($"definitelyGreaterThan: definitelyGreaterThan(a, b)");
Console.WriteLine($"definitelyLessThan: definitelyLessThan(a, b)");
Console.WriteLine();
Console.WriteLine($"HEquals: HEquals(a, b)");
Console.WriteLine($"HGreaterThan: HGreaterThan(a, b)");
Console.WriteLine($"HLessThan: HLessThan(a, b)");


I get the expected output:



a: 1.000000000000000
b: 1.000001000000000
a - b: -0.000000953674300
b - a: 0.000000953674300

approximatelyEqual: True
essentiallyEqual: True
definitelyGreaterThan: False
definitelyLessThan: False

HEquals: True
HGreaterThan: False
HLessThan: False


But if I try this:



 float a = 0.0f; 
float b = 1e-6f;


I get this:



a: 0.000000000000000
b: 0.000001000000000
a - b: -0.000001000000000
b - a: 0.000001000000000

approximatelyEqual: False
essentiallyEqual: False
definitelyGreaterThan: False
definitelyLessThan: True

HEquals: True
HGreaterThan: False
HLessThan: False


But I would have expected the same result?




The H-functions is my own naive implementations:



public static bool HEquals(float a, float b)

return Math.Abs(a - b) < Accuracy;


public static bool HGreaterThan(float a, float b)

return a - b >= Accuracy;


public static bool HLessThan(float a, float b)

return a - b <= -Accuracy;




Disclaimer: I'm not using Unity3ds Mathf.Abs() but System.Math.Abs()






share|improve this answer



















  • 1




    As I stated, I took the comparison functions straight out of the book. (Page 218) here is a link: github.com/djtrack16/thyme/blob/master/computer%20science/… Maybe it was over the top. For almost most cases your functions will work and are much cheaper. So to be complete I should add the functionality as it is, but I very much like the simpler approach. Maybe add both?
    – Binary Impact BIG
    Apr 6 at 8:21










Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191324%2fhandling-float-discrepancy-in-unity3d%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













I can't quite figure out in which situation you would use approximatelyEqual() and in which essentiallyEqual(). and what the difference really should be.



I made some tests and overall it seems to work. I did though find the below strange behavior:



Testing with these two values:



 float a = 1.0f; 
float b = 1.0f + 1e-6f;

Console.WriteLine($"a: a:F15");
Console.WriteLine($"b: b:F15");
Console.WriteLine($"a - b: a - b:F15");
Console.WriteLine($"b - a: b - a:F15");
Console.WriteLine();

Console.WriteLine($"approximatelyEqual: approximatelyEqual(a, b)");
Console.WriteLine($"essentiallyEqual: essentiallyEqual(a, b)");
Console.WriteLine($"definitelyGreaterThan: definitelyGreaterThan(a, b)");
Console.WriteLine($"definitelyLessThan: definitelyLessThan(a, b)");
Console.WriteLine();
Console.WriteLine($"HEquals: HEquals(a, b)");
Console.WriteLine($"HGreaterThan: HGreaterThan(a, b)");
Console.WriteLine($"HLessThan: HLessThan(a, b)");


I get the expected output:



a: 1.000000000000000
b: 1.000001000000000
a - b: -0.000000953674300
b - a: 0.000000953674300

approximatelyEqual: True
essentiallyEqual: True
definitelyGreaterThan: False
definitelyLessThan: False

HEquals: True
HGreaterThan: False
HLessThan: False


But if I try this:



 float a = 0.0f; 
float b = 1e-6f;


I get this:



a: 0.000000000000000
b: 0.000001000000000
a - b: -0.000001000000000
b - a: 0.000001000000000

approximatelyEqual: False
essentiallyEqual: False
definitelyGreaterThan: False
definitelyLessThan: True

HEquals: True
HGreaterThan: False
HLessThan: False


But I would have expected the same result?




The H-functions is my own naive implementations:



public static bool HEquals(float a, float b)

return Math.Abs(a - b) < Accuracy;


public static bool HGreaterThan(float a, float b)

return a - b >= Accuracy;


public static bool HLessThan(float a, float b)

return a - b <= -Accuracy;




Disclaimer: I'm not using Unity3ds Mathf.Abs() but System.Math.Abs()






share|improve this answer



















  • 1




    As I stated, I took the comparison functions straight out of the book. (Page 218) here is a link: github.com/djtrack16/thyme/blob/master/computer%20science/… Maybe it was over the top. For almost most cases your functions will work and are much cheaper. So to be complete I should add the functionality as it is, but I very much like the simpler approach. Maybe add both?
    – Binary Impact BIG
    Apr 6 at 8:21














up vote
2
down vote













I can't quite figure out in which situation you would use approximatelyEqual() and in which essentiallyEqual(). and what the difference really should be.



I made some tests and overall it seems to work. I did though find the below strange behavior:



Testing with these two values:



 float a = 1.0f; 
float b = 1.0f + 1e-6f;

Console.WriteLine($"a: a:F15");
Console.WriteLine($"b: b:F15");
Console.WriteLine($"a - b: a - b:F15");
Console.WriteLine($"b - a: b - a:F15");
Console.WriteLine();

Console.WriteLine($"approximatelyEqual: approximatelyEqual(a, b)");
Console.WriteLine($"essentiallyEqual: essentiallyEqual(a, b)");
Console.WriteLine($"definitelyGreaterThan: definitelyGreaterThan(a, b)");
Console.WriteLine($"definitelyLessThan: definitelyLessThan(a, b)");
Console.WriteLine();
Console.WriteLine($"HEquals: HEquals(a, b)");
Console.WriteLine($"HGreaterThan: HGreaterThan(a, b)");
Console.WriteLine($"HLessThan: HLessThan(a, b)");


I get the expected output:



a: 1.000000000000000
b: 1.000001000000000
a - b: -0.000000953674300
b - a: 0.000000953674300

approximatelyEqual: True
essentiallyEqual: True
definitelyGreaterThan: False
definitelyLessThan: False

HEquals: True
HGreaterThan: False
HLessThan: False


But if I try this:



 float a = 0.0f; 
float b = 1e-6f;


I get this:



a: 0.000000000000000
b: 0.000001000000000
a - b: -0.000001000000000
b - a: 0.000001000000000

approximatelyEqual: False
essentiallyEqual: False
definitelyGreaterThan: False
definitelyLessThan: True

HEquals: True
HGreaterThan: False
HLessThan: False


But I would have expected the same result?




The H-functions is my own naive implementations:



public static bool HEquals(float a, float b)

return Math.Abs(a - b) < Accuracy;


public static bool HGreaterThan(float a, float b)

return a - b >= Accuracy;


public static bool HLessThan(float a, float b)

return a - b <= -Accuracy;




Disclaimer: I'm not using Unity3ds Mathf.Abs() but System.Math.Abs()






share|improve this answer



















  • 1




    As I stated, I took the comparison functions straight out of the book. (Page 218) here is a link: github.com/djtrack16/thyme/blob/master/computer%20science/… Maybe it was over the top. For almost most cases your functions will work and are much cheaper. So to be complete I should add the functionality as it is, but I very much like the simpler approach. Maybe add both?
    – Binary Impact BIG
    Apr 6 at 8:21












up vote
2
down vote










up vote
2
down vote









I can't quite figure out in which situation you would use approximatelyEqual() and in which essentiallyEqual(). and what the difference really should be.



I made some tests and overall it seems to work. I did though find the below strange behavior:



Testing with these two values:



 float a = 1.0f; 
float b = 1.0f + 1e-6f;

Console.WriteLine($"a: a:F15");
Console.WriteLine($"b: b:F15");
Console.WriteLine($"a - b: a - b:F15");
Console.WriteLine($"b - a: b - a:F15");
Console.WriteLine();

Console.WriteLine($"approximatelyEqual: approximatelyEqual(a, b)");
Console.WriteLine($"essentiallyEqual: essentiallyEqual(a, b)");
Console.WriteLine($"definitelyGreaterThan: definitelyGreaterThan(a, b)");
Console.WriteLine($"definitelyLessThan: definitelyLessThan(a, b)");
Console.WriteLine();
Console.WriteLine($"HEquals: HEquals(a, b)");
Console.WriteLine($"HGreaterThan: HGreaterThan(a, b)");
Console.WriteLine($"HLessThan: HLessThan(a, b)");


I get the expected output:



a: 1.000000000000000
b: 1.000001000000000
a - b: -0.000000953674300
b - a: 0.000000953674300

approximatelyEqual: True
essentiallyEqual: True
definitelyGreaterThan: False
definitelyLessThan: False

HEquals: True
HGreaterThan: False
HLessThan: False


But if I try this:



 float a = 0.0f; 
float b = 1e-6f;


I get this:



a: 0.000000000000000
b: 0.000001000000000
a - b: -0.000001000000000
b - a: 0.000001000000000

approximatelyEqual: False
essentiallyEqual: False
definitelyGreaterThan: False
definitelyLessThan: True

HEquals: True
HGreaterThan: False
HLessThan: False


But I would have expected the same result?




The H-functions is my own naive implementations:



public static bool HEquals(float a, float b)

return Math.Abs(a - b) < Accuracy;


public static bool HGreaterThan(float a, float b)

return a - b >= Accuracy;


public static bool HLessThan(float a, float b)

return a - b <= -Accuracy;




Disclaimer: I'm not using Unity3ds Mathf.Abs() but System.Math.Abs()






share|improve this answer















I can't quite figure out in which situation you would use approximatelyEqual() and in which essentiallyEqual(). and what the difference really should be.



I made some tests and overall it seems to work. I did though find the below strange behavior:



Testing with these two values:



 float a = 1.0f; 
float b = 1.0f + 1e-6f;

Console.WriteLine($"a: a:F15");
Console.WriteLine($"b: b:F15");
Console.WriteLine($"a - b: a - b:F15");
Console.WriteLine($"b - a: b - a:F15");
Console.WriteLine();

Console.WriteLine($"approximatelyEqual: approximatelyEqual(a, b)");
Console.WriteLine($"essentiallyEqual: essentiallyEqual(a, b)");
Console.WriteLine($"definitelyGreaterThan: definitelyGreaterThan(a, b)");
Console.WriteLine($"definitelyLessThan: definitelyLessThan(a, b)");
Console.WriteLine();
Console.WriteLine($"HEquals: HEquals(a, b)");
Console.WriteLine($"HGreaterThan: HGreaterThan(a, b)");
Console.WriteLine($"HLessThan: HLessThan(a, b)");


I get the expected output:



a: 1.000000000000000
b: 1.000001000000000
a - b: -0.000000953674300
b - a: 0.000000953674300

approximatelyEqual: True
essentiallyEqual: True
definitelyGreaterThan: False
definitelyLessThan: False

HEquals: True
HGreaterThan: False
HLessThan: False


But if I try this:



 float a = 0.0f; 
float b = 1e-6f;


I get this:



a: 0.000000000000000
b: 0.000001000000000
a - b: -0.000001000000000
b - a: 0.000001000000000

approximatelyEqual: False
essentiallyEqual: False
definitelyGreaterThan: False
definitelyLessThan: True

HEquals: True
HGreaterThan: False
HLessThan: False


But I would have expected the same result?




The H-functions is my own naive implementations:



public static bool HEquals(float a, float b)

return Math.Abs(a - b) < Accuracy;


public static bool HGreaterThan(float a, float b)

return a - b >= Accuracy;


public static bool HLessThan(float a, float b)

return a - b <= -Accuracy;




Disclaimer: I'm not using Unity3ds Mathf.Abs() but System.Math.Abs()







share|improve this answer















share|improve this answer



share|improve this answer








edited May 2 at 6:10


























answered Apr 5 at 18:21









Henrik Hansen

3,8931417




3,8931417







  • 1




    As I stated, I took the comparison functions straight out of the book. (Page 218) here is a link: github.com/djtrack16/thyme/blob/master/computer%20science/… Maybe it was over the top. For almost most cases your functions will work and are much cheaper. So to be complete I should add the functionality as it is, but I very much like the simpler approach. Maybe add both?
    – Binary Impact BIG
    Apr 6 at 8:21












  • 1




    As I stated, I took the comparison functions straight out of the book. (Page 218) here is a link: github.com/djtrack16/thyme/blob/master/computer%20science/… Maybe it was over the top. For almost most cases your functions will work and are much cheaper. So to be complete I should add the functionality as it is, but I very much like the simpler approach. Maybe add both?
    – Binary Impact BIG
    Apr 6 at 8:21







1




1




As I stated, I took the comparison functions straight out of the book. (Page 218) here is a link: github.com/djtrack16/thyme/blob/master/computer%20science/… Maybe it was over the top. For almost most cases your functions will work and are much cheaper. So to be complete I should add the functionality as it is, but I very much like the simpler approach. Maybe add both?
– Binary Impact BIG
Apr 6 at 8:21




As I stated, I took the comparison functions straight out of the book. (Page 218) here is a link: github.com/djtrack16/thyme/blob/master/computer%20science/… Maybe it was over the top. For almost most cases your functions will work and are much cheaper. So to be complete I should add the functionality as it is, but I very much like the simpler approach. Maybe add both?
– Binary Impact BIG
Apr 6 at 8:21












 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191324%2fhandling-float-discrepancy-in-unity3d%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?