ieee-754

Formatting doubles for output in C#

核能气质少年 提交于 2019-11-26 02:19:41
问题 Running a quick experiment related to Is double Multiplication Broken in .NET? and reading a couple of articles on C# string formatting, I thought that this: { double i = 10 * 0.69; Console.WriteLine(i); Console.WriteLine(String.Format(\" {0:F20}\", i)); Console.WriteLine(String.Format(\"+ {0:F20}\", 6.9 - i)); Console.WriteLine(String.Format(\"= {0:F20}\", 6.9)); } Would be the C# equivalent of this C code: { double i = 10 * 0.69; printf ( \"%f\\n\", i ); printf ( \" %.20f\\n\", i ); printf

Coercing floating-point to be deterministic in .NET?

有些话、适合烂在心里 提交于 2019-11-26 01:45:47
I've been reading a lot about floating-point determinism in .NET, i.e. ensuring that the same code with the same inputs will give the same results across different machines. Since .NET lacks options like Java's fpstrict and MSVC's fp:strict, the consensus seems to be that there is no way around this issue using pure managed code. The C# game AI Wars has settled on using Fixed-point math instead, but this is a cumbersome solution. The main issue appears to be that the CLR allows intermediate results to live in FPU registers that have higher precision than the type's native precision, leading to

Is floating-point math consistent in C#? Can it be?

大兔子大兔子 提交于 2019-11-26 01:29:39
问题 No, this is not another \"Why is (1/3.0)*3 != 1\" question. I\'ve been reading about floating-points a lot lately; specifically, how the same calculation might give different results on different architectures or optimization settings. This is a problem for video games which store replays, or are peer-to-peer networked (as opposed to server-client), which rely on all clients generating exactly the same results every time they run the program - a small discrepancy in one floating-point

Which is the first integer that an IEEE 754 float is incapable of representing exactly?

南笙酒味 提交于 2019-11-26 01:21:05
问题 For clarity, if I\'m using a language that implements IEE 754 floats and I declare: float f0 = 0.f; float f1 = 1.f; ...and then print them back out, I\'ll get 0.0000 and 1.0000 - exactly. But IEEE 754 isn\'t capable of representing all the numbers along the real line. Close to zero, the \'gaps\' are small; as you get further away, the gaps get larger. So, my question is: for an IEEE 754 float, which is the first (closest to zero) integer which cannot be exactly represented? I\'m only really

Coercing floating-point to be deterministic in .NET?

徘徊边缘 提交于 2019-11-26 01:08:06
问题 I\'ve been reading a lot about floating-point determinism in .NET, i.e. ensuring that the same code with the same inputs will give the same results across different machines. Since .NET lacks options like Java\'s fpstrict and MSVC\'s fp:strict, the consensus seems to be that there is no way around this issue using pure managed code. The C# game AI Wars has settled on using Fixed-point math instead, but this is a cumbersome solution. The main issue appears to be that the CLR allows

Float and double datatype in Java

女生的网名这么多〃 提交于 2019-11-26 00:36:21
问题 The float data type is a single-precision 32-bit IEEE 754 floating point and the double data type is a double-precision 64-bit IEEE 754 floating point. What does it mean? And when should I use float instead of double or vice-versa? 回答1: The Wikipedia page on it is a good place to start. To sum up: float is represented in 32 bits, with 1 sign bit, 8 bits of exponent, and 23 bits of the significand (or what follows from a scientific-notation number: 2.33728*10 12 ; 33728 is the significand).

Why is NaN not equal to NaN? [duplicate]

﹥>﹥吖頭↗ 提交于 2019-11-25 23:05:20
问题 This question already has answers here : What is the rationale for all comparisons returning false for IEEE754 NaN values? (13 answers) Closed 5 years ago . The relevant IEEE standard defines a numeric constant NaN (not a number) and prescribes that NaN should compare as not equal to itself. Why is that? All the languages I\'m familiar with implement this rule. But it often causes significant problems, for example unexpected behavior when NaN is stored in a container, when NaN is in the data

What is the rationale for all comparisons returning false for IEEE754 NaN values?

亡梦爱人 提交于 2019-11-25 22:18:18
问题 Why do comparisons of NaN values behave differently from all other values? That is, all comparisons with the operators ==, <=, >=, <, > where one or both values is NaN returns false, contrary to the behaviour of all other values. I suppose this simplifies numerical computations in some way, but I couldn\'t find an explicitly stated reason, not even in the Lecture Notes on the Status of IEEE 754 by Kahan which discusses other design decisions in detail. This deviant behavior is causing trouble

Large numbers erroneously rounded in JavaScript

狂风中的少年 提交于 2019-11-25 21:58:44
问题 See this code: <html> <head> <script src=\"http://www.json.org/json2.js\" type=\"text/javascript\"></script> <script type=\"text/javascript\"> var jsonString = \'{\"id\":714341252076979033,\"type\":\"FUZZY\"}\'; var jsonParsed = JSON.parse(jsonString); console.log(jsonString, jsonParsed); </script> </head> <body> </body> </html> When I see my console in Firefox 3.5, the value of jsonParsed is: Object id=714341252076979100 type=FUZZY I.e the number is rounded. Tried different values, the same