floating-accuracy

Different results from similar floating-point functions

…衆ロ難τιáo~ 提交于 2019-12-02 08:09:25
问题 so i have 2 functions that should do the same thing float ver1(float a0, float a1) { float r0 = a0 - a1; if (abs(r0) > PI) { if (r0 > 0) { r0 -= PI2; } else { r0 += PI2; } } return r0; } float ver2(float a0, float a1) { float a2 = a1 - PI2; float r0 = a0 - a1; float r1 = a0 - a2; if (abs(r0) < abs(r1)) { return r0; } if (abs(r0) > abs(r1)) { return r1; } return 0; } note: PI and PI2 are float constants of pi and 2*pi The odd thing is that sometimes they produce different results, for example

c# string to float conversion invalid?

旧时模样 提交于 2019-12-02 07:37:20
问题 var x = dr["NationalTotal"].ToString(); gives me 333333333 var xxx = Convert.ToSingle(dr["NationalTotal"].ToString()); gives me 333333344 Any ideas why? 回答1: The floating point specification says that the 32 bit representation of a floating point number is Thus, the largest integer that can be represented without loss of (integer) accuracy is 16777216 (0x1000000). A simple test program to convince you that this is so: #include <stdio.h> int main(void) { float x; unsigned long j; j =

c# string to float conversion invalid?

余生颓废 提交于 2019-12-02 06:59:11
var x = dr["NationalTotal"].ToString(); gives me 333333333 var xxx = Convert.ToSingle(dr["NationalTotal"].ToString()); gives me 333333344 Any ideas why? The floating point specification says that the 32 bit representation of a floating point number is Thus, the largest integer that can be represented without loss of (integer) accuracy is 16777216 (0x1000000). A simple test program to convince you that this is so: #include <stdio.h> int main(void) { float x; unsigned long j; j = 0x00FFFFFC; int i; x = j; for(i=0; i<10;i++) printf("%ld + %d = %f\n", j, i, x+i); } Output: 16777212 + 0 = 16777212

How to avoid floating point arithmetics issues?

别等时光非礼了梦想. 提交于 2019-12-02 06:45:35
Python (and almost anything else) has known limitations while working with floating point numbers (nice overview provided here ). While problem is described well in the documentation it avoids providing any approach to fixing it. And with this question I am seeking to find a more or less robust way to avoid situations like the following: print(math.floor(0.09/0.015)) # >> 6 print(math.floor(0.009/0.0015)) # >> 5 print(99.99-99.973) # >> 0.016999999999825377 print(.99-.973) # >> 0.017000000000000015 var = 0.009 step = 0.0015 print(var < math.floor(var/step)*step+step) # False print(var < (math

c++ fmod returns 0.2 for fmod(0.6,0.2)

落爺英雄遲暮 提交于 2019-12-02 05:29:19
问题 when I use fmod(0.6,0.2) in c++ it returns 0.2 I know this is caused by floating point accuracy but it seems I have to get remainder of two double this moment thanks very much for any solutions for this kind of problem 回答1: The mathematical values 0.6 and 0.2 cannot be represented exactly in binary floating-point. This demo program will show you what's going on: #include <iostream> #include <iomanip> #include <cmath> int main() { const double x = 0.6; const double y = 0.2; std::cout << std:

Why would a variable of type double have an unexpected result?

偶尔善良 提交于 2019-12-02 04:45:12
My sanity check fails because a double variable does not contain the expected result, it's really bizarre. double a = 1117.54 + 8561.64 + 13197.37; double b = 22876.55; Console.WriteLine("{0} == {1}: {2}", a, b, a == b); Gives us this output: 22876.55 == 22876.55: False Further inspection shows us that variable a, in fact, contains the value 22876.550000000003. This is reproducible in vb.net as well. Am I sane ? What is going on? Floating point types are not always capable of accurately representing their exact decimal values. It's either a "known bug" or "by design", depending on your

Different results from similar floating-point functions

陌路散爱 提交于 2019-12-02 04:41:13
so i have 2 functions that should do the same thing float ver1(float a0, float a1) { float r0 = a0 - a1; if (abs(r0) > PI) { if (r0 > 0) { r0 -= PI2; } else { r0 += PI2; } } return r0; } float ver2(float a0, float a1) { float a2 = a1 - PI2; float r0 = a0 - a1; float r1 = a0 - a2; if (abs(r0) < abs(r1)) { return r0; } if (abs(r0) > abs(r1)) { return r1; } return 0; } note: PI and PI2 are float constants of pi and 2*pi The odd thing is that sometimes they produce different results, for example if you feed them 0.28605145 and 5.9433694 then the first one results in 0.62586737 and the second one

F# - How to compare floats

别说谁变了你拦得住时间么 提交于 2019-12-02 03:00:37
In F#. How to efficiently compare floats for equality that are almost equal? It should work for very large and very small values too. I am thinking of first comparing the Exponent and then the Significand (Mantissa) while ignoring the last 4 bits of the its 52 bits. Is that a good approach? How can I get the Exponent and Significand of a float? An F# float is just a shorthand for System.Double . That being the case, you can use the BitConverter.DoubleToInt64Bits method to efficiently (and safely! ) "cast" an F# float value to int64 ; this is useful because it avoids allocating a byte[] , as

C++ floating point accuracy in while loops

孤街醉人 提交于 2019-12-02 01:10:53
I am trying to count the amount of dollar and coin denominations in a grand total by using a series of while loops. When I get down to the coins however, I am off by a penny. When I enter say 99.95, I get the output 3 quarters, 1 dime, 1 nickel, and 4 pennies. I've narrowed the problem down to a floating point accuracy issue. However all the solutions I've researched haven't been applicable in my situation. Any pointers? #include <iostream> using namespace std; int main() { float amount; cout<<"enter amount" << endl; cin>>amount; int pennies=0, nickels=0, dimes=0, quarters=0, ones=0, fives=0,

Floating point behavior in Python 2.6 vs 2.7

我怕爱的太早我们不能终老 提交于 2019-12-01 23:15:46
问题 So I break out the Python 2.6 interpreter and I get this: Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 2.1 2.1000000000000001 >>> 2.2 2.2000000000000002 >>> 2.2 2.2000000000000002 However in Python 2.7 I get more human-like results like below: Python 2.7.5+ (default, Sep 19 2013, 13:48:49) [GCC 4.8.1] on linux2 Type "help", "copyright", "credits" or "license" for