floating-point

C: difference between (int)x and floor(x)?

In C, what is the difference between these two? float myF = 5.6; printf( "%i \n", (int)myF ); // gives me "5" printf( "%ld \n", floor(myF) ); // also "5"? When is one preferable over the other? ...

Division to the nearest 1 decimal place without floating point math?

I am having some speed issues with my C# program and identified that this percentage calculation is causing a slow down. The calculation is simply n/d * 100. Both the numerator and denominator can be any integer number. The numerator can never be greater than the denominator and is never negative. Therefore, the result is always from ...

C99 and floating point environment

Hi, I was looking at new features of C99 and saw the floating point environment: #include <fenv.h> My question is simple. If I'm performing floating point number computations, do I have to include the above preprocessor directive in my code? If no, then what does this directive do and when does it become important to include? Thanks ...

Are all the system's floating points operations the same?

We're making this web app in PHP and when working in the reports we have Excel files to compare our results to make sure our coding is doing the right operations. Now we're running into some differences due floating point arithmetics. We're doing the same divisions and multiplications and running into slightly different numbers, that ad...

Floating point arithmetic is too reliable.

I understand that floating point arithmetic as performed in modern computer systems is not always consistent with real arithmetic. I am trying to contrive a small C# program to demonstrate this. eg: static void Main(string[] args) { double x = 0, y = 0; x += 20013.8; x += 20012.7; y += 10016.4; ...

How to output floating point numbers with a custom output format in C++?

The problem is that I want to output Mathematica compatible floating point numbers. The only difference with the standard IOStream or printf output format is that the exponential e is replaced by *^: Standard C/C++ output format: 1.23e-4 Mathematica format: 1.23*^-4 Is there a way to manipulate streams to achieve this effect? My origi...

Floating point conversion from Fixed point algorithm

Hi, I have an application which is using 24 bit fixed point calculation.I am porting it to a hardware which does support floating point, so for speed optimization I need to convert all fixed point based calculation to floating point based calculation. For this code snippet, It is calculating mantissa for(i=0;i<8207;i++) { // Do n^8/...

php calculate float HELP!!

help!! i have weird math calculation here. hope someone will explain. $a = 1.85/100; $b = 1.5/100; $c = 1.1/100; $d = 0.4/100; $e = 0.4/100; $f = 0.4/100; $g = 0.4/100; $h = $a + $b + $c + $d + $e + $f + $g; echo $h*100 ."<br>"; $i = $h-$a; $i = $i-$b; $i = $i-$c; $i = $i-$d; $i = $i-$e; $i = $i-$f; $i = $i-$g; echo $i; last $i val...

Nicely representing a floating-point number in python

I want to represent a floating-point number as a string rounded to some number of significant digits, and never using the exponential format. Essentially, I want to display any floating-point number and make sure it looks nice. There are several parts to this problem: I need to be able to specify the number of significant digits. The ...

The most efficient way of implementing pow() function in floating point

Hi, I am trying to implement my own version of pow() and sqrt() function as my custom library doesn't have pow()/sqrt() floating point support. Can anyone help? ...

Will fixed-point arithmetic be worth my trouble?

I'm working on a fluid dynamics Navier-Stokes solver that should run in real time. Hence, performance is important. Right now, I'm looking at a number of tight loops that each account for a significant fraction of the execution time: there is no single bottleneck. Most of these loops do some floating-point arithmetic, but there's a lot ...

Why is there no better representation for floating points than sign and magnitude?

We have 2's complement for integers that allows us to perform operations without worrying about the sign. That is a big help at the implementation level. Similarly we have so many floating point operations and yet we rely on sign and magnitude. What is the reason? Why can't a 2's complement like system work for floats? ...

Problem using the find function in MATLAB

I have two arrays of data that I'm trying to amalgamate. One contains actual latencies from an experiment in the first column (e.g. 0.345, 0.455... never more than 3 decimal places), along with other data from that experiment. The other contains what is effectively a 'look up' list of latencies ranging from 0.001 to 0.500 in 0.001 increm...

Swap bits in c++ for a double

Im trying to change from big endian to little endian on a double. One way to go is to use double val, tmp = 5.55; ((unsigned int *)&val)[0] = ntohl(((unsigned int *)&tmp)[1]); ((unsigned int *)&val)[1] = ntohl(((unsigned int *)&tmp)[0]); But then I get a warning: "dereferencing type-punned pointer will break strict-aliasing rules" an...

How does Double.isNaN() work?

The sun jdk implementation looks like this: return v != v; Can anyone explain how that works? ...

Iterate through all possible floating-point values

How can I list all possible values of a floating-point data type? I can do this using a union in C or C++ but will that be portable? How can this be done in other languages? Javascript? Let's just assume I am using this iteration to map theta to sin(theta). ...

Is there a floating point value of x, for which x-x == 0 is false?

In most cases, I understand that a floating point comparison test should be implemented using over a range of values (abs(x-y) < epsilon), but does self subtraction imply that the result will be zero? // can the assertion be triggered? float x = //?; assert( x-x == 0 ) My guess is that nan/inf might be special cases, but I'm more int...

Problem with Precision floating point operation in C

Hi Guys, For one of my course project I started implementing "Naive Bayesian classifier" in C. My project is to implement a document classifier application (especially Spam) using huge training data. Now I have problem implementing the algorithm because of the limitations in the C's datatype. ( Algorithm I am using is given here, htt...

How can I add floats together in different orders, and always get the same total?

Let's say I have three 32-bit floating point values, a, b, and c, such that (a + b) + c != a + (b + c). Is there a summation algorithm, perhaps similar to Kahan summation, that guarantees that these values can be summed in any order and always arrive at the exact same (fairly accurate) total? I'm looking for the general case (i.e. not a ...

how IEEE-754 floating point numbers work

Let's say I have this: float i = 1.5 in binary, this float is represented as: 0 01111111 10000000000000000000000 I broke up the binary to represent the 'signed', 'exponent' and 'fraction' chunks. What I don't understand is how this represents 1.5. The exponent is 0 once you subtract the bias (127 - 127), and the fraction part with...