floating-accuracy

Fast Exp calculation: possible to improve accuracy without losing too much performance?

笑着哭i 提交于 2019-11-27 18:34:46
I am trying out the fast Exp(x) function that previously was described in this answer to an SO question on improving calculation speed in C#: public static double Exp(double x) { var tmp = (long)(1512775 * x + 1072632447); return BitConverter.Int64BitsToDouble(tmp << 32); } The expression is using some IEEE floating point "tricks" and is primarily intended for use in neural sets. The function is approximately 5 times faster than the regular Math.Exp(x) function. Unfortunately, the numeric accuracy is only -4% -- +2% relative to the regular Math.Exp(x) function, ideally I would like to have

Why 0.1 + 0.2 == 0.3 in D?

旧街凉风 提交于 2019-11-27 17:29:10
assert(0.1 + 0.2 != 0.3); // shall be true is my favorite check that a language uses native floating point arithmetic. C++ #include <cstdio> int main() { printf("%d\n", (0.1 + 0.2 != 0.3)); return 0; } Output: 1 http://ideone.com/ErBMd Python print(0.1 + 0.2 != 0.3) Output: True http://ideone.com/TuKsd Other examples Java: http://ideone.com/EPO6X C#: http://ideone.com/s14tV Why is this not true for D? As understand D uses native floating point numbers. Is this a bug? Do they use some specific number representation? Something else? Pretty confusing. D import std.stdio; void main() { writeln(0.1

If dealing with money in a float is bad, then why does money_format() do it?

心已入冬 提交于 2019-11-27 14:32:55
问题 I've been waffling on how to deal with currency display and math in PHP, and for a long time have been storing it in MySQL using the DECIMAL type, and using money_format() to format it for display on the web page. However, today I looked at the actual prototype: string money_format ( string $format , float $number ) I'm a little confused now. All I've been told is, avoid floats for money! But here it is, the fundamental formatting function (say that five times fast), casting the input to a

Truncate float numbers with PHP

眉间皱痕 提交于 2019-11-27 14:25:36
When a float number needs to be truncated to a certain digit after the floating point, it turns out that it is not easy to be done. For example if the truncating has to be done to second digit after the point, the numbers should be 45.8976 => 45.89, 0.0185 => 0.01 ( second digit after the point is not rounded according to the third digit after the point ). Functions like round() , number_format() , sprintf() round the number and print out 45.8976 => 45.90, 0.0185 => 0.02 I have met two solutions and I am wondering if they are good enough and which one is better to be used 1. function

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

痴心易碎 提交于 2019-11-27 10:26:34
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') The simple answer is because 3*0.1 != 0.3 due to quantization (roundoff) error (whereas 4*0.1 == 0.4 because

Avg of float inconsistency

断了今生、忘了曾经 提交于 2019-11-27 09:38:19
The select returns right at 23,000 rows The except will return between 60 to 200 rows (and not the same rows) The except should return 0 as it is select a except select a PK: [docSVenum1].[enumID], [docSVenum1].[valueID], [FTSindexWordOnce].[wordID] [tf] is a float and and I get float is not exact But I naively thought avg(float) would be repeatable Avg(float) does appear to be repeatable What is the solution? TF is between 0 and 1 and I only need like 5 significant digits I just need avg(TF) to be the same number run to run Decimal(9,8) gives me enough precision and if I cast to decimal(9,8)

Integers and float precision

与世无争的帅哥 提交于 2019-11-27 09:09:41
This is more of a numerical analysis rather than programming question, but I suppose some of you will be able to answer it. In the sum two floats, is there any precision lost? Why? In the sum of a float and a integer, is there any precision lost? Why? Thanks. In the sum two floats, is there any precision lost? If both floats have differing magnitude and both are using the complete precision range (of about 7 decimal digits) then yes, you will see some loss in the last places. Why? This is because floats are stored in the form of (sign) (mantissa) × 2 (exponent) . If two values have differing

Why don't I get zero when I subtract the same floating point number from itself in Perl? [duplicate]

拜拜、爱过 提交于 2019-11-27 08:39:51
问题 This question already has answers here : Closed 9 years ago . Possible Duplicates: Why is floating point arithmetic in C# imprecise? Why does ghci say that 1.1 + 1.1 + 1.1 > 3.3 is True? #!/usr/bin/perl $l1 = "0+0.590580+0.583742+0.579787+0.564928+0.504538+0.459805+0.433273+0.384211+0.3035810"; $l2 = "0+0.590580+0.583742+0.579788+0.564928+0.504538+0.459805+0.433272+0.384211+0.3035810"; $val1 = eval ($l1); $val2 = eval ($l2); $diff = (($val1 - $val2)/$val1)*100; print " (($val1 - $val2)/$val1)

double or float datatype doesn't addup properly in a loop?

一曲冷凌霜 提交于 2019-11-27 08:34:01
问题 In a loop I am adding 0.10 till I reach the desired #, and getting the index. This is my code : private static int getIndexOfUnits(float units) { int index = -1; float addup = 0.10f; for(float i = 1.00f; i < units; i=(float)i+addup) { index++; System.out.println("I = " + i + " Index = " + index); } return index; } If the units passed is 5.7, the ourput I see is : I = 1.0 Index = 0 I = 1.1 Index = 1 I = 1.2 Index = 2 I = 1.3000001 Index = 3 I = 1.4000001 Index = 4 I = 1.5000001 Index = 5 I = 1

Floating point precision while using Python's max()

荒凉一梦 提交于 2019-11-27 08:21:59
问题 Why so? >>> max(2, 2.01) 2.0099999999999998 回答1: The number 2.01 represented in binary is: b10.00000010100011111100001010001111110000101000111111000010100011111100... The computer uses only a finite number of digits to store floating-point values, but the binary representation of 2.01 requires infinitely many digits; as a result, it is rounded to the closest representable value: b10.000000101000111111000010100011111100001010001111110 Expressed in decimal, this number is exactly: 2