floating-point

F# Floating point ranges are experimental and may be deprecated

I'm trying to make a little function to interpolate between two values with a given increment. [ 1.0 .. 0.5 .. 20.0 ] The compiler tells me that this is deprecated, and suggests using ints then casting to float. But this seems a bit long-winded if I have a fractional increment - do I have to divide my start and end values by my increm...

Best way to parse a string to a float with both "," and "." as fraction separator? (For a number < 5)

This is kind of related to my previous question, but not really. I have an input of which I don't know the culture. So it can both use ',' and '.' as separator for the fraction. The number will never be >5 though, so we can be rather sure if there's a separator, it will be for the fraction. I was looking at the TryParse method. It accep...

extracting mantissa and exponent from double in c#

Is there any straightforward way to get the mantissa and exponent from a double in c# (or .NET in general)? I found this example using google, but I'm not sure how robust it would be. Could the binary representation for a double change in some future version of the framework, etc? The other alternative I found was to use System.Decima...

How best to sum up lots of floating point numbers?

Imagine you have a large array of floating point numbers, of all kinds of sizes. What is the most correct way to calculate the sum, with the least error? For example, when the array looks like this: [1.0, 1e-10, 1e-10, ... 1e-10.0] and you add up from left to right with a simple loop, like sum = 0 numbers.each do |val| sum += val...

How do I display the binary representation of a float or double?

I'd like to display the binary (or hexadecimal) representation of a floating point number. I know how to convert by hand (using the method here), but I'm interested in seeing code samples that do the same. Although I'm particularly interested in the C++ and Java solutions, I wonder if any languages make it particularly easy so I'm maki...

Why are c/c++ floating point types so oddly named?

C++ offers three floating point types: float, double, and long double. I infrequently use floating-point in my code, but when I do, I'm always caught out by warnings on innocuous lines like float PiForSquares = 4.0; The problem is that the literal 4.0 is a double, not a float - Which is irritating. For integer types, we have short in...

Floating Point Limitations

My code: a = '2.3' I wanted to display a as a floating point value. Since a is a string, I tried: float(a) The result I got was : 2.2999999999999998 I want a solution for this problem. Please, kindly help me. I was following this tutorial. ...

How to speed up floating-point to integer number conversion?

We're doing a great deal of floating-point to integer number conversions in our project. Basically, something like this for(int i = 0; i < HUGE_NUMBER; i++) int_array[i] = float_array[i]; The default C function which performs the conversion turns out to be quite time consuming. Is there any work around (maybe a hand tuned functi...

What are coding conventions for using floating-point in Linux device drivers?

This is related to this question. I'm not an expert on Linux device drivers or kernel modules, but I've been reading "Linux Device Drivers" [O'Reilly] by Rubini & Corbet and a number of online sources, but I haven't been able to find anything on this specific issue yet. When is a kernel or driver module allowed to use floating-point r...

AlmostEqual2sComplement or BigDecimal Scaling

Using Java: The more I read on floating point value comparison, the more I get lost. In case of currency which is usually gets rounded off, what whoule you prefer..using Epsilon or a Big Decimal comparison with scaling? For instance, your data would range from 0.00 - 49,999.99? ...

Does floor() return something that's exactly representable?

In C89, floor() returns a double. Is the following guaranteed to work? double d = floor(3.0 + 0.5); int x = (int) d; assert(x == 3); My concern is that the result of floor might not be exactly representable in IEEE 754. So d gets something like 2.99999, and x ends up being 2. For the answer to this question to be yes, all integers ...

"Approximate" greatest common divisor

Suppose you have a list of floating point numbers that are approximately multiples of a common quantity, for example 2.468, 3.700, 6.1699 which are approximately all multiples of 1.234. How would you characterize this "approximate gcd", and how would you proceed to compute or estimate it? Strictly related to my answer to this questio...

python limiting floats to two decimal points

I want a to be rounded to 13.95 >>> a 13.949999999999999 >>> round(a, 2) 13.949999999999999 The round function does not work [the way I expect]. ...

What is the best way to extract load average float values from a string in Python?

If I have a string such as "17:31:51 up 134 days, 11:26, 1 user, load average: 0.22, 0.15, 0.10" what is the best way to extract just the x3 load average values at the end? I have written a regexp that does this but is this the most efficient / fastest method? >>> s = "17:31:51 up 134 days, 11:26, 1 user, load average: 0.22, 0.15,...

Floating Point to Binary Value(C++)

I want to take a floating point number in C++, like 2.25125, and a int array filled with the binary value that is used to store the float in memory (IEEE 754). So I could take a number, and end up with a int num[16] array with the binary value of the float: num[0] would be 1 num[1] would be 1 num[2] would be 0 num[3] would be 1 and so o...

strange results of simple floating point operations - bad FPU internal state?

I have a software project in which I sometimes get strange results from small, simple floating point operations. I assume there is something I have missed, and would like some tips about how to debug the following problems: (the compiler used is MS VC 6.0, that is version 12 of the Microsoft C compiler) First anomaly: extern double ...

C#.NET: Is it safe to check floating point values for equality to 0?

I know you can't rely on equality between double or decimal type values normally, but I'm wondering if 0 is a special case. While I can understand imprecisions between 0.00000000000001 and 0.00000000000002, 0 itself seems pretty hard to mess up since it's just nothing. If you're imprecise on nothing, it's not nothing anymore. But I do...

round() for float in C++

I need a simple floating point rounding function, thus: double round(double); round(0.1) = 0 round(-0.1) = 0 round(-0.9) = -1 I can find ceil() and floor() in the math.h - but not round(). Is it present in the standard C++ library under another name, or is it missing?? ...

Extract decimal part from a floating point number in C

How can we extract the decimal part of a floating point number and to store the decimal part and the integer part into seperate two integer variables? ...

How to deal with excess precision in floating-point computations?

In my numerical simulation I have code similar to the following snippet double x; do { x = /* some computation */; } while (x <= 0.0); /* some algorithm that requires x to be (precisely) larger than 0 */ With certain compilers (e.g. gcc) on certain platforms (e.g. linux, x87 math) it is possible that x is computed in higher than dou...