precision

Issue with precision of Ruby math operations

◇◆丶佛笑我妖孽 提交于 2019-12-31 04:15:09
问题 Do you know how to fix the following issue with math precision? p RUBY_VERSION # => "1.9.1" p 0.1%1 # => 0.1 p 1.1%1 # => 0.1 p 90.0%1 # => 0.0 p 90.1%1 # => 0.0999999999999943 p 900.1%1 # => 0.100000000000023 p RUBY_VERSION # => "1.9.2" p 0.1%1 # => 0.1 p 1.1%1 # => 0.10000000000000009 p 90.0%1 # => 0.0 p 90.1%1 # => 0.09999999999999432 p 900.1%1 # => 0.10000000000002274 回答1: Big Decimal As the man said; Squeezing infinitely many real numbers into a finite number of bits requires an

acos(double) gives different result on x64 and x32 Visual Studio

回眸只為那壹抹淺笑 提交于 2019-12-31 04:00:06
问题 acos(double) gives different result on x64 and x32 Visual Studio. printf("%.30g\n", double(acosl(0.49990774364240564))); printf("%.30g\n", acos(0.49990774364240564)); on x64: 1.0473040763868076 on x32: 1.0473040763868078 on linux4.4 x32 and x64 with sse enabled: 1.0473040763868078 is there a way to make VSx64 acos() give me 1.0473040763868078 as result? 回答1: TL:DR: this is normal and you can't reasonably change it. The 32-bit library may be using 80-bit FP values in x87 registers for its

Fast SSE low precision exponential using double precision operations

与世无争的帅哥 提交于 2019-12-30 09:07:35
问题 I am looking for for a fast-SSE-low-precision (~1e-3) exponential function. I came across this great answer: /* max. rel. error = 3.55959567e-2 on [-87.33654, 88.72283] */ __m128 FastExpSse (__m128 x) { __m128 a = _mm_set1_ps (12102203.0f); /* (1 << 23) / log(2) */ __m128i b = _mm_set1_epi32 (127 * (1 << 23) - 298765); __m128i t = _mm_add_epi32 (_mm_cvtps_epi32 (_mm_mul_ps (a, x)), b); return _mm_castsi128_ps (t); } Based on the work of Nicol N. Schraudolph: N. N. Schraudolph. "A fast,

Matlab vs C++ Double Precision

纵饮孤独 提交于 2019-12-28 06:53:28
问题 I am porting some code from Matlab to C++. In Matlab format long D = 0.689655172413793 (this is 1.0 / 1.45) E = 2600 / D // I get E = 3.770000000000e+03 In C++ double D = 0.68965517241379315; //(this is 1.0 / 1.45) double E = 2600 / D; //I get E = 3769.9999999999995 It is a problem for me because in both cases I have to do rounding down to 0 (Matlab's fix), and in the first case (Matlab) is becomes 3770, whereas in the second case (C++) it becomes 3769. I realise that it is because of the two

What class to use for money representation?

安稳与你 提交于 2019-12-28 06:28:24
问题 What class should I use for representation of money to avoid most rounding errors? Should I use Decimal , or a simple built-in number ? Is there any existing Money class with support for currency conversion that I could use? Any pitfalls that I should avoid? 回答1: I assume that you talking about Python. http://code.google.com/p/python-money/ "Primitives for working with money and currencies in Python" - the title is self explanatory :) 回答2: Never use a floating point number to represent money.

What class to use for money representation?

ぐ巨炮叔叔 提交于 2019-12-28 06:28:02
问题 What class should I use for representation of money to avoid most rounding errors? Should I use Decimal , or a simple built-in number ? Is there any existing Money class with support for currency conversion that I could use? Any pitfalls that I should avoid? 回答1: I assume that you talking about Python. http://code.google.com/p/python-money/ "Primitives for working with money and currencies in Python" - the title is self explanatory :) 回答2: Never use a floating point number to represent money.

gfortran REAL not accurate to 8 decimal places [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-25 20:00:31
问题 This question already exists : gfortran represents REAL incorrectly [duplicate] Closed 5 years ago . This question has not been previously answered. I am trying to represent a real or any number for that matter in Fortran correctly. What gfortran is doing for me is way off. For example when I declare the variable REAL pi=3.14159 fortran prints pi = 3.14159012 rather than say 3.14159000. See below: PROGRAM Test IMPLICIT NONE REAL:: pi = 3.14159 PRINT *, "PI = ",pi END PROGRAM Test This prints:

Setting up precision C++

时光怂恿深爱的人放手 提交于 2019-12-25 18:11:10
问题 So, i'm attempting to set the precision for input values in my code. I want the values to be printed with two decimal points afterwards of precision though i'm not exactly sure how. Here's my code. #include <iostream> #include <iomanip> float uphill, wellD, waterLvl, buckVol; float buckAscRate, downHill, volume; float last; float timeReq; int scene = 1; void timeRequired() { std::setw(2); std::setprecision(2); std::cout << "Scenario " << scene << ":" << std::endl; std::cout << "up hill" << "

Precise value of double in decimal and floating?

不想你离开。 提交于 2019-12-25 09:04:22
问题 DBL_MAX is 179769313486231570814527423731704356798070567525844996598917476803157260 780028538760589558632766878171540458953514382464234321326889464182768467 546703537516986049910576551282076245490090389328944075868508455133942304 583236903222948165808559332123348274797826204144723168738177180919299881 250404026184124858368.000000 But if I do: double a=1234567890123456789.0; printf("%f",a); 1234567890123456768.000000 Here the precision is 17 digits. double a=0.1234567890123456789; printf("%

Float and double precision in C

限于喜欢 提交于 2019-12-25 06:52:13
问题 In C, double has more precision than float, and according to "C primerplus sixth edition" book (page 80), a float can represent at least 6 significant figures and a double can represent at least 13 significant figures. So I tried to verify that with this simple example: #include<stdio.h> int main(void){ float a = 3.3333333; // 7 significant digits double b = 3.33333333333333;// 14 significant digits printf("\nFloat: %f\n", a); printf("Double: %f\n", b); return 0; } And this is the output of