floating-accuracy

IEEE 754: How exactly does it work?

烂漫一生 提交于 2019-12-09 18:38:31
问题 Why does the following code behave as it does in C? float x = 2147483647; //2^31 printf("%f\n", x); //Outputs 2147483648 Here is my thought process: 2147483647 = 0 1001 1101 1111 1111 1111 1111 1111 111 (0.11111111111111111111111)base2 = (1-(0.5)^23)base10 => (1.11111111111111111111111)base2 = (1 + 1-(0.5)^23)base10 = (1.99999988)base10 Therefore, to convert the IEEE 754 notation back to decimal: 1.99999988 * 2^30 = 2147483520 So technically, the C program must have printed out 2147483520,

Why does GDB evaluate floating-point arithmetic differently from C++?

浪子不回头ぞ 提交于 2019-12-09 08:51:53
问题 I've encountered something a little confusing while trying to deal with a floating-point arithmetic problem. First, the code. I've distilled the essence of my problem into this example: #include <iostream> #include <iomanip> using namespace std; typedef union {long long ll; double d;} bindouble; int main(int argc, char** argv) { bindouble y, z, tau, xinum, xiden; y.d = 1.0d; z.ll = 0x3fc5f8e2f0686eee; // double 0.17165791262311053 tau.ll = 0x3fab51c5e0bf9ef7; // double 0.053358253178712838 //

Payne Hanek algorithm implementation in C

可紊 提交于 2019-12-09 07:00:05
问题 I'm struggling to understand how TO IMPLEMENT the range reduction algorithm published by Payne and Hanek (range reduction for trigonometric functions) I've seen there's this library: http://www.netlib.org/fdlibm/ But it looks to me so twisted, and all the theoretical explanation i've founded are too simple to provide an implementation. Is there some good... good... good explanation of it? 回答1: Performing argument reduction for trigonometric functions via the Payne-Hanek algorithm is actually

Why is the output of inv() and pinv() not equal in Matlab and Octave?

孤街醉人 提交于 2019-12-09 03:29:11
问题 I have noticed that if A is a NxN matrix and it has the inverse matrix. But what the inv() and pinv() function output is different. - My environment is Win7x64 SP1, Matlab R2012a, Cygwin Octave 3.6.4, FreeMat 4.2 Have a look at the examples from Octave: A = rand(3,3) A = 0.185987 0.192125 0.046346 0.140710 0.351007 0.236889 0.155899 0.107302 0.300623 pinv(A) == inv(A) ans = 0 0 0 0 0 0 0 0 0 It's all the same ans result by running the same command above in Matlab. And I calculate inv(A)*A or

Why do I need 17 significant digits (and not 16) to represent a double?

谁说我不能喝 提交于 2019-12-08 23:13:27
问题 Can someone give me an example of a floating point number (double precision), that needs more than 16 significant decimal digits to represent it? I have found in this thread that sometimes you need up to 17 digits, but I am not able to find an example of such a number (16 seems enough to me). Can somebody clarify this? Thanks a lot! 回答1: My other answer was dead wrong. #include <stdio.h> int main(int argc, char *argv[]) { unsigned long long n = 1ULL << 53; unsigned long long a = 2*(n-1);

Invertability of IEEE 754 floating-point division

白昼怎懂夜的黑 提交于 2019-12-08 14:42:11
问题 What is the invertability of the IEEE 754 floating-point division? I mean is it guaranteed by the standard that if double y = 1.0 / x then x == 1.0 / y , i.e. x can be restored precisely bit by bit? The cases when y is infinity or NaN are obvious exceptions. 回答1: Yes, there are IEEE 754 double-precision(*) values x that are such x != 1.0 / (1.0 / x) . It is easy to build an example of a normal value with this property by hand: the one that's written 0x1.fffffffffffffp0 in C99's hexadecimal

issue with float increment in Android

不问归期 提交于 2019-12-08 12:26:54
问题 I am trying to increment value of float by 0.1 every time. But i am getting very strange result some time. float kmVal = 0.0f; EditText kms = (EditText) findViewById(R.id.km); kmVal = Float.valueOf(kms.getText().toString()); On Click of button I am incrementing the value. kmVal += 0.1; kms.setText(String.valueOf(kmVal)); LOG :- 06-24 13:16:21.644: I/System.out(958): Value ==0.1 06-24 13:16:21.644: I/System.out(958): String Value ==0.1 06-24 13:16:21.886: D/SntpClient(61): request time failed:

How to force 32bits floating point calculation consistency across different platforms?

戏子无情 提交于 2019-12-08 06:32:31
问题 I have a simple piece of code that operates with floating points. Few multiplications, divisions, exp(), subtraction and additions in a loop. When I run the same piece of code on different platforms (like PC, Android phones, iPhones) I get slightly different results. The result is pretty much equal on all the platforms but has a very small discrepancy - typically 1/1000000 of the floating point value. I suppose the reason is that some phones don't have floating point registers and just

C++ I've just read that floats are inexact and do not store exact integer values. What does this mean?

北战南征 提交于 2019-12-08 01:59:35
问题 I am thinking of this at a binary level. would a float of value 1 and an integer of value 1 not compile down to (omitting lots of zeros here) 0001 If they do both compile down to this then where does this inexactness come in. Resource I'm using is http://www.cprogramming.com/tutorial/lesson1.html Thanks. 回答1: It's possible. Floating point numbers are represented in an exponential notation (a*2^n), where some bits represent a (the significand ), and some bits represent n (the exponent ). You

Dealing with small numbers and accuracy

随声附和 提交于 2019-12-08 01:28:08
问题 I have a program where I deal with a lot of very small numbers (towards the lower end of the Double limits). During the execution of my application, some of these numbers progressively get smaller meaning their "estimation" is less accurate. My solution at the moment is scaling them up before I do any calculations and then scaling them back down again? ...but it's got me thinking, am I actually gaining any more "accuracy" by doing this? Thoughts? 回答1: Are your numbers really in the region