floating-accuracy

Trying to write a code for finding the machine epsilon

I am trying to find out the precision level for various floating point formats in C (i.e. float, double and long double). Here is the code I'm using at the moment: #include <stdio.h> #define N 100000 int main(void) { float max = 1.0, min = 0.0, test; int i; /* Counter for the conditional loop */ ...

nth root of a number

I wrote a program to calculate nth root of a number upto 2 decimal places. eg 4th root of 81 is 3., 3rd root of 125 is 5.Its working nicely except for the 2nd root of 4. It's giving the output 1.99 instead of 2. Here is the code. #include<stdio.h> int main(int argc, char **argv) { double root1(int,int); int n; int num1; ...

C# .Net double issue... 6.8 != 6.8?

I was doing some unit testing at work and a peculiar error popped up for one of the assertions. Note that expectedValue and actualValue are both doubles. Assert.AreEqual(expectedValue, actualValue); The exception stated that they were not equal, elaborating that "expected value: <6.8> actual value: <6.8>." The expected value is a har...

Rounding in PHP

$a = ((0.1 + 0.7) * 10) == (int)((0.1 + 0.7) * 10); PHP returns false. Could anybody explain me, why that happens? First returns 8, second 7. ...

Ruby print out a float with optional digits

Hi all, I'm trying to print out a float number, that has the value 5 What I get when it print it out is 5.0, so my question is: How to i make it so, that if the value is just 5, it will just print 5 without the .0 after it, and if it is 5.2 or so, it will print out that? I've looked around, but all I found was to either force it one ...

Why see -0,000000000000001 in access query?

I have an sql: SELECT Sum(Field1), Sum(Field2), Sum(Field1)+Sum(Field2) FROM Table GROUP BY DateField HAVING Sum(Field1)+Sum(Field2)<>0; Problem is sometimes Sum of field1 and field2 is value like: 9.5-10.3 and the result is -0,800000000000001. Could anybody explain why this happens and how to solve it? ...

PHP Math Precision

$a = '35'; $b = '-34.99'; echo ($a + $b); Results in 0.009999999999998 What is up with that? I wondered why my program kept reporting odd results. Why doesn't PHP return the expected 0.01? ...

Real number arithmetic in a general purpose language?

As (hopefully) most of you know, floating point arithmetic is different from real number arithmetic. It's for starters imprecise. Many numbers, especially decimals (0.1, 0.3) cannot be represented, leading to problems like this. A more thorough list can be found here. Are there any general purpose languages that have built-in support fo...

Addition vs Subtraction in loss of significance with floating-points

While learning about precision in floating point arithmetic and different methods to avoid it (using a conjugate, taylor series,...) books frequently mention the subtraction of two very similar numbers or one large and one small number as the biggest cause of error. How come it is only subtraction that causes this and not addition? As I ...

Is it possible to increase accuracy of floating point arithemtic with gcc?

Hi, some program in C which does extensive floating point calculations get right results on a pc linux box, but wrong results on the SPE of the cell processor, but not on the PPU of the cell. I am using gcc compilers. I wonder if there is some gcc compilation option to increase rounding method or similar so I get single float precision ...

Floating point comparison - Result between different runs

I know that I can not compare two floating point or double numbers for absolute equality on C++/C. If for some reason, I write a if condition which uses the absolute equality, is it guaranteed that the if condition will return the same result on different runs of the program for same data? Or it is purely non-deterministic and the result...

why is 1.2 * 30 = 35?

Why does this: int main(void) { short w = 30; return 1.2 * w; } return 35? ...

C++ vs Python precision

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...

Some questions about floating points

I'm wondering if a number is represented one way in a floating point representation, is it going to be represented in the same way in a representation that has a larger size. That is, if a number has a particular representation as a float, will it have the same representation if that float is cast to a double and then still the same when...

Finding the smallest integer that can not be represented as an IEEE-754 32 bit float

Possible Duplicate: Which is the first integer that an IEEE 754 float is incapable of representing exactly? Firstly, this IS a homework question, just to clear this up immediately. I'm not looking for a spoon fed solution of course, just maybe a little pointer to the right direction. So, my task is to find the smallest positi...

Why don't operations on double-precision values give expected results?

System.out.println(2.14656); 2.14656 System.out.println(2.14656%2); 0.14656000000000002 WTF? ...

splitting the bill algorithmically & fair, afterwards :)

I'm trying to solve the following real-life problem you might have encountered yourselves: You had dinner with some friends and you all agreed to split the bill evenly. Except that when the bill finally arrives, you find out not everyone has enough cash on them (if any, cheap bastards). So, some of you pays more than others... Afterwa...

define double constant as hexadecimal?

Hi, I would like to have the closest number below 1.0 as a floating point. By reading wikipedia's article on IEEE-745 I have managed to find out that the binary representation for 1.0 is 3FF0000000000000, so the closest double value is actually 0x3FEFFFFFFFFFFFFF. The only way I know of to initialize a double with this binary data is th...

How does MySQL handle exact IEEE 754 representations?

Is there a way to load exact octal, hex or binary representations of IEEE 754 floats (as text) into a MySQL table using the correct single- or double-precision data type? E.g. the number pi written as 1078530011 or 40490fdb (single-precision). Preferably with LOAD DATA INFILE/mysqlimport, in the interests of speed... ...