floating-accuracy

Math “pow” in Java and C# return slightly different results?

与世无争的帅哥 提交于 2019-12-05 19:08:36
问题 I am porting program from C# to java. I've faced a fact that Java Math.pow(0.392156862745098,1./3.) = 0.7319587495200227 C# Math.Pow( 0.392156862745098, 1.0 / 3.0) =0.73195874952002271 this last digit leads to sufficient differences in further calculations. Is there any way to emulate c#'s pow? Thanx 回答1: Just to confirm what Chris Shain wrote, I get the same binary values: // Java public class Test { public static void main(String[] args) { double input = 0.392156862745098; double pow = Math

Change in Python built in round() function between 2.4 and 2.7

。_饼干妹妹 提交于 2019-12-05 17:08:26
Has the built in round() function in Python changed between 2.4 and 2.7? Python 2.4: Python 2.4.6 (#1, Feb 12 2009, 14:52:44) [GCC 3.4.6 20060404 (Red Hat 3.4.6-8)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> f = 1480.39499999999998181010596454143524169921875 >>> round(f,2) 1480.4000000000001 >>> Python 2.7: Python 2.7.1 (r271:86832, May 13 2011, 08:14:41) [GCC 3.4.6 20060404 (Red Hat 3.4.6-11)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> f = 1480.39499999999998181010596454143524169921875 >>> round(f, 2) 1480.39

Why 4.1%2 returns 0.0999999999999996 using Ruby?But 4.2%2==0.2

徘徊边缘 提交于 2019-12-05 10:18:05
Why 4.1%2 returns 0.0999999999999996?But 4.2%2==0.2. See here: What Every Programmer Should Know About Floating-Point Arithmetic Real numbers are infinite. Computers are working with a finite number of bits (32 bits, 64 bits today). As a result floating-point arithmetic done by computers cannot represent all the real numbers. 0.1 is one of these numbers. Note that is not an issue related to Ruby, but to all programming languages because it comes from the way computers represent real numbers. Float s can not always be represented exactly, see What Every Programmer Should Know About Floating

Does std::hash guarantee equal hashes for “equal” floating point numbers?

懵懂的女人 提交于 2019-12-05 07:00:21
Is the floating point specialisation of std::hash (say, for double s or float s) reliable regarding almost-equality ? That is, if two values (such as (1./std::sqrt(5.)/std::sqrt(5.)) and .2 ) should compare equal but will not do so with the == operator, how will std::hash behave? So, can I rely on a double as an std::unordered_map key to work as expected? I have seen " Hashing floating point values " but that asks about boost; I'm asking about the C++11 guarantees. std::hash has same guarantees for all types over which it can be instantiated: if two objects are equal, their hash codes will be

C++ vs Python precision

心已入冬 提交于 2019-12-05 06:07:19
Trying out a problem of finding the first k digits of a num^num I wrote the same program in C++ and Python C++ long double intpart,num,f_digit,k; cin>>num>>k; f_digit= pow(10.0,modf(num*log10(num),&intpart)+k-1); cout<<f_digit; Python (a,b) = modf(num*log10(num)) f_digits = pow(10,b+k-1) print f_digits Input 19423474 9 Output C++ > 163074912 Python > 163074908 I checked the results the C++ solution is the accurate one. Checked it at http://www.wolframalpha.com/input/?i=19423474^19423474 Any idea how can I get the same precision in Python ??? EDIT : I know about the external library packages to

Accurate computation of PDF of standard normal distribution using C standard math library

眉间皱痕 提交于 2019-12-05 02:11:58
问题 The probability density function of the standard normal distribution is defined as e -x 2 /2 / √(2π). This can be rendered in straightforward manner into C code. A sample single-precision implementation might be: float my_normpdff (float a) { return 0x1.988454p-2f * my_expf (-0.5f * a * a); /* 1/sqrt(2*pi) */ } While this code is free from premature underflow, there is an accuracy issue since the error incurred in the computation of a 2 /2 is magnified by the subsequent exponentiation. One

How to convert strings to floats with perfect accuracy?

岁酱吖の 提交于 2019-12-05 01:47:36
I'm trying to write a function in the D programming language to replace the calls to C's strtold. (Rationale: To use strtold from D, you have to convert D strings to C strings, which is inefficient. Also, strtold can't be executed at compile time.) I've come up with an implementation that mostly works, but I seem to lose some precision in the least significant bits. The code to the interesting part of the algorithm is below and I can see where the precision loss comes from, but I don't know how to get rid of it. (I've left out a lot of the parts of code that weren't relevant to the core

C# wrong subtraction? 12.345 - 12 = 0.345000000000001 [closed]

谁说我不能喝 提交于 2019-12-05 01:32:49
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center . Closed 7 years ago . I am beginner in C# and I am working with floating point numbers. I need to do subtraction between these two numbers but it does not work. I know it is caused by floating point number, but how can I fix it please and if you be so good can you explain me

How to divide tiny double precision numbers correctly without precision errors?

隐身守侯 提交于 2019-12-05 00:55:43
I'm trying to diagnose and fix a bug which boils down to X/Y yielding an unstable result when X and Y are small: In this case, both cx and patharea increase smoothly. Their ratio is a smooth asymptote at high numbers, but erratic for "small" numbers. The obvious first thought is that we're reaching the limit of floating point accuracy, but the actual numbers themselves are nowhere near it. ActionScript "Number" types are IEE 754 double-precision floats, so should have 15 decimal digits of precision (if I read it right). Some typical values of the denominator (patharea): 0.0000000002119123 0

Does Fortran have inherent limitations on numerical accuracy compared to other languages?

假如想象 提交于 2019-12-05 00:24:16
问题 While working on a simple programming exercise, I produced a while loop (DO loop in Fortran) that was meant to exit when a real variable had reached a precise value. I noticed that due to the precision being used, the equality was never met and the loop became infinite. This is, of course, not unheard of and one is advised that, rather than comparing two numbers for equality, it is best see if the absolute difference between two numbers is less than a set threshold. What I found disappointing