Due to the difficulty with which the machine accurately represents floating point values, we use the technique from Write Great Code: Understanding the machine to perform floating point comparisons:
from the editor: please insert your code here. See HTML comment in the question source
Currently, we have hardcoded the "error" value. But the error is different on different machines. Is there any good way to find out the error for a particular machine instead of hard tolerance coding?
See my answer to What is the smallest nonzero positive floating point number in Perl? .
, . .
64- IEEE 754 , , 1.0 + e != 1.0 e = 2.2204460492503131e-016. (DBL_EPSILON float.h C, std::numeric_limits<double>().epsilon() <limits> ++). , , 64- IEEE 754.
1.0 + e != 1.0
e = 2.2204460492503131e-016
DBL_EPSILON
std::numeric_limits<double>().epsilon()
<limits>
, , . , 1e-300 + 2e-300 , 1 + 1e-300 == 1. 1e30 + 1 == 1e30.
1e-300 + 2e-300
1 + 1e-300 == 1
1e30 + 1 == 1e30
, , , . , . , , ,
" ", , epsilon.
epsilon
, - *:
float number = 1.0; // precision of this number float epsilon = 1.0; while ((number + epsilon) != number) { epsilon /= 2.0f; }
(*This is off the top of my head. Don't use this code without checking.)
.
And one more here: Floating point: precision machine with theory and equations for calculating epsilon.
Source: https://habr.com/ru/post/1714834/More articles:Help with transcript C - cSharpSVN gets post-commit-hook using 'SvnLookClient' - c #Selecting a subset of a list with a special item first in the list - c #How to display html content converted to xaml flowdocument in wpf application - c #WPF Frame Management - c #Более быстрый/более сжатый способ определить правильный размер, необходимый для хранения подписанных/неподписанных ints? - erlangJQuery UI - output of one element from the screen: hidden element when dragging and dropping - jqueryHow can I access the DOM using WebKit.NET? - domRegular expression for checking patch length field with packed space - regexWhy is NHibernate a simple choice in the HQL database for SQLite not working? - sqliteAll Articles