precision

How do I truncate the significand of a floating point number to an arbitrary precision in Java? [duplicate]

自古美人都是妖i 提交于 2019-12-13 09:19:46
问题 This question already has answers here : Efficient way to round double precision numbers to a lower precision given in number of bits (2 answers) Closed last year . I would like to introduce some artificial precision loss into two numbers being compared to smooth out minor rounding errors so that I don't have to use the Math.abs(x - y) < eps idiom in every comparison involving x and y . Essentially, I want something that behaves similarly to down-casting a double to a float and then up

C++ different output in double and float [duplicate]

China☆狼群 提交于 2019-12-13 07:56:49
问题 This question already has answers here : What is the difference between float and double? (11 answers) Closed last year . I have a sample program: #include <iostream> #include <stdio.h> using namespace std; int main() { float a = 33.30; double b = 33.30; char a1[1024]; char b1[1024]; sprintf(a1, "%0.6f", a); sprintf(b1, "%0.6lf", b); cout << a1 << endl; cout << b1 << endl; return 0; } The output I am getting is: 33.299999 33.300000 I am getting correct result for double and incorrect for

C float and double comparisons

人盡茶涼 提交于 2019-12-13 07:19:04
问题 I'm comparing simple floats and doubles in C, specifically the value 8.7 for both of them. Now I assign 8.7 to each variable, when I print I get a result of 8.7000 for both values. Why has the compiler added these zeros. And the main question I wanted to ask was is there any further numbers that I'm not seeing, as in hidden after the trailing zeros. I read that I shouldn't do comparisons like this with float because of a lack of precision, but I thought with such a small value surely it can

Losing precision converting from double to int in Java

霸气de小男生 提交于 2019-12-13 07:07:52
问题 I tried to test the following code, and it gave me a 434 on n, the result which I did not anticipate, what's the reason for this loss of precision? double f = 4.35; int n = (int) (100 * f); // n will be 434 - why? n = (int) Math.round(100*f); // n will be 435 回答1: Floating point isn't perfect. It uses approximated values. In particular, double s can not represent all numbers. Just like 1/3 can't be precisely represented in decimal using a finite number of digits, so too can't 4.35 be

Changing the number precision in Oracle [duplicate]

☆樱花仙子☆ 提交于 2019-12-13 04:27:16
问题 This question already has answers here : Changing precision of numeric column in Oracle (3 answers) Closed 5 years ago . I haven't seen this exact issue... I apologize if I missed it. I have a database I inherited from the company that created it when they lost the contract to my company. Now, the customer has an "urgent" issue to change the precision of a value. They want to change a number from 10.3 to 10.8; the field is defined in the database as 10,3. In my research of the issue, It seems

How to set the output precision to 2 decimal places in C++?

爷,独闯天下 提交于 2019-12-13 02:29:40
问题 I want to globally set the ouptut precision to 2 decimal places. I already tried to use iomanip and setprecision , however I keep getting output with 'e' in it. This is my example code: #include <iostream> #include <iomanip> using namespace std; int main () { double pay=16.78; double hours; double total; cout.precision(2); cout << "Please enter how many hours you worked : " << endl; cin >> hours; total=hours*pay; cout << "You earned: " << total << endl; } 回答1: I'm not familiar with "cout

Odd results using the COMBIN function

大兔子大兔子 提交于 2019-12-13 02:10:09
问题 I am a mathematics student and was given a proof detailed in this link. Out of curiosity I tried setting that up in a an Excel table with columns as N and rows as K, so that: =COMBIN(R$6,$B16)-COMBIN(R$6-1,$B16)-COMBIN(R$6-1,$B16-1) should equal 0 . For the most part it does but sometimes I get odd results. For example: =COMBIN(110,35)-COMBIN(110-1,35)-COMBIN(110-1,35-1)=0 and =COMBIN(110,45)-COMBIN(110-1,45)-COMBIN(110-1,45-1)=0 but =COMBIN(110,40)-COMBIN(110-1,40)-COMBIN(110-1,40-1)

How to use fmod and avoid precision issues

我怕爱的太早我们不能终老 提交于 2019-12-13 01:57:08
问题 I'm going to boil this problem down to the simplest form: Let's iterate from [0 .. 5.0] with a step of 0.05 and print out 'X' for every 0.25 multiplier. for(double d=0.0; d<=5.0; d+=0.05) { if(fmod(d,0.25) is equal 0) print 'X'; } This will of course not work since d will be [0, 0.05000000001, 0.100000000002, ...] causing fmod() to fail. Extreme example is when d=1.999999999998 and fmod(d,0.25) = 1 . How to tackle this? Here is an editable online example. 回答1: I'd solve this by simply not

numpy `arange` exceeds end value?

安稳与你 提交于 2019-12-13 01:51:01
问题 I had expected numpy's arange(start,end) to produce values in the range [start,end]. The following example demonstrates that that's not always true (the final value is larger than end ): import numpy as np start=2e9 end=start+321 step=0.066833171999 x=np.arange(start,end,step=step) print x[-1]>end # Prints "True" print x[-1]-end # Prints 0.00013661384582519531 The error seems far too large to be caused by machine precision (but perhaps I'm thinking about it incorrectly). What's going on? PS:

Symmetrical Lerp & compiler optimizations

感情迁移 提交于 2019-12-12 21:31:13
问题 I had a function: float lerp(float alpha, float x0, float x1) { return (1.0f - alpha) * x0 + alpha * x1; } For those who haven't seen it, this is preferable to x0 + (x1-x0) * alpha because the latter doesn't guarantee that lerp(1.0f, x0, x1) == x1 . Now, I want my lerp function to have an additional property: I'd like lerp(alpha, x0, x1) == lerp(1-alpha, x1, x0) . (As for why: this is a toy example of a more complicated function.) The solution I came up with that seems to work is float lerp