Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
IEEE 754-1985
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Comparing floating-point numbers== Every possible bit combination is either a NaN or a number with a unique value in the [[affinely extended real number system]] with its associated order, except for the two combinations of bits for negative zero and positive zero, which sometimes require special attention (see below). The [[#Representation_of_numbers|binary representation]] has the special property that, excluding NaNs, any two numbers can be compared as [[sign and magnitude]] integers ([[endianness]] issues apply). When comparing as [[2's-complement]] integers: If the sign bits differ, the negative number precedes the positive number, so 2's complement gives the correct result (except that negative zero and positive zero should be considered equal). If both values are positive, the 2's complement comparison again gives the correct result. Otherwise (two negative numbers), the correct FP ordering is the opposite of the 2's complement ordering. Rounding errors inherent to floating point calculations may limit the use of comparisons for checking the exact equality of results. Choosing an acceptable range is a complex topic. A common technique is to use a comparison epsilon value to perform approximate comparisons.<ref>{{cite web|url=https://github.com/godotengine/godot/blob/master/core/math/math_funcs.h#L302|title=Godot math_funcs.h|website=GitHub.com|date=30 July 2022 }}</ref> Depending on how lenient the comparisons are, common values include <code>1e-6</code> or <code>1e-5</code> for single-precision, and <code>1e-14</code> for double-precision.<ref>{{cite web|url=https://github.com/godotengine/godot/blob/master/core/math/math_defs.h#L34|title=Godot math_defs.h|website=GitHub.com|date=30 July 2022 }}</ref><ref>{{cite web|url=https://github.com/godotengine/godot/blob/master/modules/mono/glue/Managed/Files/MathfEx.cs#L18|title=Godot MathfEx.cs|website=GitHub.com}}</ref> Another common technique is ULP, which checks what the difference is in the last place digits, effectively checking how many steps away the two values are.<ref>{{cite web|url=https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/|title=Comparing Floating Point Numbers, 2012 Edition|website=randomascii.wordpress.com|date=26 February 2012 }}</ref> Although negative zero and positive zero are generally considered equal for comparison purposes, some [[programming language]] [[relational operator]]s and similar constructs treat them as distinct. According to the [[Java (programming language)|Java]] Language Specification,<ref>{{cite web|url=http://java.sun.com/docs/books/jls/|title=Java Language and Virtual Machine Specifications|website=Java Documentation}}</ref> comparison and equality operators treat them as equal, but <code>Math.min()</code> and <code>Math.max()</code> distinguish them (officially starting with Java version 1.1 but actually with 1.1.1), as do the comparison methods <code>equals()</code>, <code>compareTo()</code> and even <code>compare()</code> of classes <code>Float</code> and <code>Double</code>.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
IEEE 754-1985
(section)
Add topic