floating-accuracy

What is a simple example of floating point/rounding error?

爱⌒轻易说出口 提交于 2019-11-26 12:23:35
I've heard of "error" when using floating point variables. Now I'm trying to solve this puzzle and I think I'm getting some rounding/floating point error. So I'm finally going to figure out the basics of floating point error. What is a simple example of floating point/rounding error (preferably in C++) ? Edit: For example say I have an event that has probability p of succeeding. I do this event 10 times (p does not change and all trials are independent). What is the probability of exactly 2 successful trials? I have this coded as: double p_2x_success = pow(1-p, (double)8) * pow(p, (double)2) *

How to do an integer log2() in C++?

末鹿安然 提交于 2019-11-26 12:18:29
In the C++ standard libraries I found only a floating point log method. Now I use log to find the level of an index in a binary tree ( floor(2log(index)) ). Code (C++): int targetlevel = int(log(index)/log(2)); I am afraid that for some of the edge elements (the elements with value 2^n) log will return n-1.999999999999 instead of n.0. Is this fear correct? How can I modify my statement so that it always will return a correct answer? You can use this method instead: int targetlevel = 0; while (index >>= 1) ++targetlevel; Note: this will modify index. If you need it unchanged, create another

Why does the floating-point value of 4*0.1 look nice in Python 3 but 3*0.1 doesn't?

时光怂恿深爱的人放手 提交于 2019-11-26 11:55:37
问题 I know that most decimals don\'t have an exact floating point representation (Is floating point math broken?). But I don\'t see why 4*0.1 is printed nicely as 0.4 , but 3*0.1 isn\'t, when both values actually have ugly decimal representations: >>> 3*0.1 0.30000000000000004 >>> 4*0.1 0.4 >>> from decimal import Decimal >>> Decimal(3*0.1) Decimal(\'0.3000000000000000444089209850062616169452667236328125\') >>> Decimal(4*0.1) Decimal(\'0.40000000000000002220446049250313080847263336181640625\')

Python float - str - float weirdness

喜夏-厌秋 提交于 2019-11-26 11:53:49
>>> float(str(0.65000000000000002)) 0.65000000000000002 >>> float(str(0.47000000000000003)) 0.46999999999999997 ??? What is going on here? How do I convert 0.47000000000000003 to string and the resultant value back to float? I am using Python 2.5.4 on Windows. str(0.47000000000000003) give '0.47' and float('0.47') can be 0.46999999999999997 . This is due to the way floating point number are represented (see this wikipedia article) Note: float(repr(0.47000000000000003)) or eval(repr(0.47000000000000003)) will give you the expected result, but you should use Decimal if you need precision. float

C++ floating point precision [duplicate]

拥有回忆 提交于 2019-11-26 11:53:15
Possible Duplicate: Floating point inaccuracy examples double a = 0.3; std::cout.precision(20); std::cout << a << std::endl; result: 0.2999999999999999889 double a, b; a = 0.3; b = 0; for (char i = 1; i <= 50; i++) { b = b + a; }; std::cout.precision(20); std::cout << b << std::endl; result: 15.000000000000014211 So.. 'a' is smaller than it should be. But if we take 'a' 50 times - result will be bigger than it should be. Why is this? And how to get correct result in this case? To get the correct results, don't set precision greater than available for this numeric type: #include <iostream>

Dealing with accuracy problems in floating-point numbers

雨燕双飞 提交于 2019-11-26 10:47:19
I was wondering if there is a way of overcoming an accuracy problem that seems to be the result of my machine's internal representation of floating-point numbers: For the sake of clarity the problem is summarized as: // str is "4.600"; atof( str ) is 4.5999999999999996 double mw = atof( str ) // The variables used in the columns calculation below are: // // mw = 4.5999999999999996 // p = 0.2 // g = 0.2 // h = 1 (integer) int columns = (int) ( ( mw - ( h * 11 * p ) ) / ( ( h * 11 * p ) + g ) ) + 1; Prior to casting to an integer type the result of the columns calculation is 1.9999999999999996;

Truncate (not round off) decimal numbers in javascript

六眼飞鱼酱① 提交于 2019-11-26 10:29:34
I am trying to truncate decimal numbers to decimal places. Something like this: 5.467 -> 5.46 985.943 -> 985.94 toFixed(2) does just about the right thing but it rounds off the value. I don't need the value rounded off. Hope this is possible in javascript. upd : So, after all it turned out, rounding bugs will always haunt you, no matter how hard you try to compensate them. Hence the problem should be attacked by representing numbers exactly in decimal notation. Number.prototype.toFixedDown = function(digits) { var re = new RegExp("(\\d+\\.\\d{" + digits + "})(\\d)"), m = this.toString().match

Decimal place issues with floats and decimal.Decimal

淺唱寂寞╮ 提交于 2019-11-26 09:55:38
问题 I seem to be losing a lot of precision with floats. For example I need to solve a matrix: 4.0x -2.0y 1.0z =11.0 1.0x +5.0y -3.0z =-6.0 2.0x +2.0y +5.0z =7.0 This is the code I use to import the matrix from a text file: f = open(\'gauss.dat\') lines = f.readlines() f.close() j=0 for line in lines: bits = string.split(line, \',\') s=[] for i in range(len(bits)): if (i!= len(bits)-1): s.append(float(bits[i])) #print s[i] b.append(s) y.append(float(bits[len(bits)-1])) I need to solve using gauss

How to correctly and standardly compare floats?

[亡魂溺海] 提交于 2019-11-26 08:56:07
问题 Every time I start a new project and when I need to compare some float or double variables I write the code like this one: if (fabs(prev.min[i] - cur->min[i]) < 0.000001 && fabs(prev.max[i] - cur->max[i]) < 0.000001) { continue; } Then I want to get rid of these magic variables 0.000001(and 0.00000000001 for double) and fabs, so I write an inline function and some defines: #define FLOAT_TOL 0.000001 So I wonder if there is any standard way of doing this? May be some standard header file? It

Getting the decimal part of a double in Swift

别等时光非礼了梦想. 提交于 2019-11-26 08:27:46
问题 I\'m trying to separate the decimal and integer parts of a double in swift. I\'ve tried a number of approaches but they all run into the same issue... let x:Double = 1234.5678 let n1:Double = x % 1.0 // n1 = 0.567800000000034 let n2:Double = x - 1234.0 // same result let n3:Double = modf(x, &integer) // same result Is there a way to get 0.5678 instead of 0.567800000000034 without converting to the number to a string? 回答1: Without converting it to a string, you can round up to a number of