floating-point

Floating point formatting in printf()

i have array of floats where data are stored with varying decimal points so some are 123.40000, 123.45000, 123.45600...now if i want to print these values in the string without the 0s in the end in printf() so that they are 123.4, 123.45, 123.456, without those 0s in the end. is this possible? if so how? ...

checking to ensure all values in a field are integers in MySQL

I have a column that is currently a floating-point number and I need to check if all the values in the column are integers. What's the easiest way to do this? ...

Single vs Double datatypes

Are there any situations where it would make more sense to use a single datatype instead of a double? From my searching, the disadvantage to a double is that it requires more space, which isn't a problem for most applications. In that case, should all floating point numbers be doubles? A little background info: I'm working with an app...

Rounding to use for int -> float -> int round trip conversion

I'm writing a set of numeric type conversion functions for a database engine, and I'm concerned about the behavior of converting large integral floating-point values to integer types with greater precision. Take for example converting a 32-bit int to a 32-bit single-precision float. The 23-bit significand of the float yields about 7 dec...

Basic Python Numbers!

Why does 0.1 + 0.1 + 0.1 - 0.3 evaluates to 5.5511151231257827e-17 in Python? ...

How to return a float point number with a defined number of decimal places?

So I know how to print a floating point number with a certain decimal places. My question is how to return it with a specified number of decimal places? Thanks. ...

Can 12.1 be represented exactly as a floating point number?

This is in reference to the comments in this question: This code in Java produces 12.100000000000001 and this is using 64-bit doubles which can present 12.1 exactly. – Pyrolistical Is this true? I felt that since a floating point number is represented as a sum of powers of two, you cannot represent 12.1 exactly, no matter how many...

Question with floating point addition

float totalAmount = 0; . . .//totalAmount assigned value 1.05 correctly . totalAmount += float.Parse(dataRow["Amt"].ToString()); //where dataRow["Amt"] has value 4.93 The answer I get for totalAmount is 5.97999954 instead of 5.98 Why is this happening? ...

Set the display precision of a float in Ruby

Is it possible to set the display precision of a float in Ruby? Something like: z = 1/3 z.to_s #=> 0.33333333333333 z.to_s(3) #=> 0.333 z.to_s(5) #=> 0.33333 Or do I have to override the to_s method of Float? ...

sprintf(buf, "%.20g", x) // how large should buf be?

I am converting double values to string like this: std::string conv(double x) { char buf[30]; sprintf(buf, "%.20g", x); return buf; } I have hardcoded the buffer size to 30, but am not sure if this is large enough for all cases. How can I find out the maximum buffer size I need? Does the precision get higher (and theref...

How can I check if a string can be converted to a float?

First my context is that of a compiler writer who needs to convert floating point literals (strings) into float/double values. I haven't done any floating point programming the last 15 years so i'm pretty sure this is a total stupid newbie question. double res; errno=0; *res = strtod((const char*)literal,NULL); if (errno==ERANGE) thr...

Validate double value range and step

I'm trying to construct an algorithm that validates that a double value is a member of a range defined with min, max and step values. The problem is checking that the value obeys the step rule. For integers this can be easily done: boolean validate(int value, int min, int step, int max){ return value >= min && value ...

Keeping sync in multiplayer RTS game that uses floating point arithmetic

I'm writing a 2D space RTS game in C#. Single player works. Now I want to add some multiplayer functionality. I googled for it and it seems there is only one way to have thousands of units continuously moving without a powerful net connection: send only the commands through the network while running the same simulation at every player. ...

Consistent rounding of floating points in Ruby

I understand due to the inexact representation of floating points, the following code 'feels' inconsistent. "%.1f" % 1.14 # => 1.1 "%.1f" % 1.15 # => 1.1 "%.1f" % 1.16 # => 1.2 "%.0f" % 1.4 # => 1 "%.0f" % 1.5 # => 2 "%.0f" % 1.6 # => 2 However, is there an easy way of doing consistent floating points rounding by 5? One way might be t...

invalid static assert behavior

I am trying to setup a static assert (outside the main function) with GCC v4.3.x: #define STATIC_ASSERT(cond) extern void static_assert(int arg[(cond) ? 1 : -1]) STATIC_ASSERT( (double)1 == (double)1 ); // failed but when I use float numbers, the assert always failed. Is it possible to run this static assert properly ? ...

negative precision values in ostream

This is more of a question of curiosity but does anyone know how negative precision values are handled in C++? For example: double pi = 3.14159265; cout.precision(-10); cout.setf(ios::fixed, ios::floatfield); cout << pi << endl; I've tried this out and using GCC and it seems that the precision value is ignored but I was curious if ...

Floating Point Concepts in Python

Why Does -22/10 return -3 in python. Any pointers regarding this will be helpful for me. ...

StopWatch returns Integer after DirectX Device creation

The question is pretty "simple". I have an engine that can work with both DirectX9 and DirectX10 with SlimDX in C#. I use the Stopwatch class to time the game and test speed of some routine. Strangely exactly after the creation of the DirectX9 the floating point of the StopWatch go crazy and the GetTimeStamp()/Frequency division returns ...

optimizing with IEEE floating point - guaranteed mathematical identities?

I am having some trouble with IEEE floating point rules preventing compiler optimizations that seem obvious. For example, char foo(float x) { if (x == x) return 1; else return 0; } cannot be optimized to just return 1 because NaN == NaN is false. Okay, fine, I guess. However, I want to write such that the ...

Why there is a discrepancy in the result?

If I apply Binet Formula and Recursive formula for finding the fibonaci series, there is a discrepancy in result. Why? Basically I am a student and it is our assignment to implement the fibbonacci series. So I while making the experiment I came across this situation. Thanks in advance ...