floating-accuracy

sin, cos, tan not accurate

泪湿孤枕 提交于 2020-04-29 15:52:13
问题 Why does sinl give incorrect results when the argument is near a non-zero multiple of pi? Why does sinl give incorrect results when the argument is large? The following code illustrates that. Note that the digits used to initialize variable pi do not exactly match any 64-bit long double value. The compiler chooses the nearest value, which is 3.14159265358979323851280895940618620443274267017841339111328125 . The expected sine value can be found using libquadmath, gnu MPFR lib, or an online

sin, cos, tan not accurate

柔情痞子 提交于 2020-04-29 15:51:26
问题 Why does sinl give incorrect results when the argument is near a non-zero multiple of pi? Why does sinl give incorrect results when the argument is large? The following code illustrates that. Note that the digits used to initialize variable pi do not exactly match any 64-bit long double value. The compiler chooses the nearest value, which is 3.14159265358979323851280895940618620443274267017841339111328125 . The expected sine value can be found using libquadmath, gnu MPFR lib, or an online

Rounding oddity - what is special about “100”?

假如想象 提交于 2020-01-26 10:53:52
问题 Does anyone have an explanation for this strange rounding in haskell (GHCi, version 7.2.1). Everything seems fine unless I multiply with 100. *Main> 1.1 1.1 *Main> 1.1 *10 11.0 *Main> 1.1 *100 110.00000000000001 *Main> 1.1 *1000 1100.0 *Main> 1.1 *10000 11000.0 Edit: what is puzzeling me is that the rounding error only shows when multiplying with 100. Edit(2): The comments I received made me realize, that this it totally unrelated to haskell, but a general issue with floating point numbers.

Why this same code produce two different fp results on different Machines?

送分小仙女□ 提交于 2020-01-24 18:24:13
问题 Here's the code: #include <iostream> #include <math.h> const double ln2per12 = log(2.0) / 12.0; int main() { std::cout.precision(100); double target = 9.800000000000000710542735760100185871124267578125; double unnormalizatedValue = 9.79999999999063220457173883914947509765625; double ln2per12edValue = unnormalizatedValue * ln2per12; double errorLn2per12 = fabs(target - ln2per12edValue / ln2per12); std::cout << unnormalizatedValue << std::endl; std::cout << ln2per12 << std::endl; std::cout <<

Why do these two float64s have different values?

眉间皱痕 提交于 2020-01-20 04:53:47
问题 Consider these two cases: fmt.Println(912 * 0.01) fmt.Println(float64(912) * 0.01) (Go Playground link) The second one prints 9.120000000000001, which is actually fine, I understand why that is happening. However, why does the first line print 9.12, without the …01 at the end? Does Go multiply the two untyped constants and simply replace them with a 9.12 literal when compiling? 回答1: As per spec: Constant expressions are always evaluated exactly; intermediate values and the constants

How to check dependencies of floats

折月煮酒 提交于 2020-01-19 12:41:55
问题 I want to determine (in c++) if one float number is the multiplicative inverse of another float number. The problem is that i have to use a third variable to do it. For instance this code: float x=5,y=0.2; if(x==(1/y)) cout<<"They are the multiplicative inverse of eachother"<<endl; else cout<<"They are NOT the multiplicative inverse of eachother"<<endl; will output: "they are not..." which is wrong and this code: float x=5,y=0.2,z; z=1/y; if(x==z) cout<<"They are the multiplicative inverse of

Matlab array arithmetic inaccuracy

人走茶凉 提交于 2020-01-15 12:12:21
问题 When i am trying to simulate my sine approximation in matlab i found a strange problem. The problem is that when i apply my function to an array, it returns one results, whereas applying functions to individual values ​​gives a slightly different result. I was able to get same behaviour in this example: z = single(0:0.001:1); F = @(x) (x.^2 - single(1.2342320e-001)).*x.^2; %some test function z(999) % Returns 9.9800003e-001 F(z(999)) % Returns 8.6909407e-001 temp = F(z); temp(999) % Voila! It

Matlab array arithmetic inaccuracy

淺唱寂寞╮ 提交于 2020-01-15 12:09:47
问题 When i am trying to simulate my sine approximation in matlab i found a strange problem. The problem is that when i apply my function to an array, it returns one results, whereas applying functions to individual values ​​gives a slightly different result. I was able to get same behaviour in this example: z = single(0:0.001:1); F = @(x) (x.^2 - single(1.2342320e-001)).*x.^2; %some test function z(999) % Returns 9.9800003e-001 F(z(999)) % Returns 8.6909407e-001 temp = F(z); temp(999) % Voila! It

Matlab array arithmetic inaccuracy

我们两清 提交于 2020-01-15 12:09:27
问题 When i am trying to simulate my sine approximation in matlab i found a strange problem. The problem is that when i apply my function to an array, it returns one results, whereas applying functions to individual values ​​gives a slightly different result. I was able to get same behaviour in this example: z = single(0:0.001:1); F = @(x) (x.^2 - single(1.2342320e-001)).*x.^2; %some test function z(999) % Returns 9.9800003e-001 F(z(999)) % Returns 8.6909407e-001 temp = F(z); temp(999) % Voila! It

Understanding floating point numbers in php

Deadly 提交于 2020-01-14 10:28:08
问题 I know these questions may get asked a lot but from my reading and testing it had me confused a bit and a lot of the reading I have done has just confused me more as it is quite complex. Some people seem to have issues with simple comparisons, however I have had no issues myself. For example... $num1 = 27.64; $num2 = 27.64; if ($num1 == $num2) { echo 'Good!'; } else { echo 'Bad!'; } // Echo's "Good!" ...and $num1 = 27.60; $num2 = 27.6; if ($num1 == $num2) { echo 'Good!'; } else { echo 'Bad!';