floating-point-conversion

Limit floating point precision?

*爱你&永不变心* 提交于 2019-11-27 05:59:38
问题 Is there a way to round floating points to 2 points? E.g.: 3576.7675745342556 becomes 3576.76 . 回答1: round(x * 100) / 100.0 If you must keep things floats: roundf(x * 100) / 100.0 Flexible version using standard library functions: double GetFloatPrecision(double value, double precision) { return (floor((value * pow(10, precision) + 0.5)) / pow(10, precision)); } 回答2: If you are printing it out, instead use whatever print formatting function available to you. In c++ cout << setprecision(2) <<

C - Serialization of the floating point numbers (floats, doubles)

本秂侑毒 提交于 2019-11-27 01:52:33
How to convert a floating point number into a sequence of bytes so that it can be persisted in a file? Such algorithm must be fast and highly portable. It must allow also the opposite operation, deserialization. It would be nice if only very tiny excess of bits per value (persistent space) is required. Assuming you're using mainstream compilers, floating point values in C and C++ obey the IEEE standard and when written in binary form to a file can be recovered in any other platform, provided that you write and read using the same byte endianess. So my suggestion is: pick an endianess of choice

Number of significant digits for a floating point type

匆匆过客 提交于 2019-11-26 20:43:01
问题 The description for type float in C mentions that the number of significant digits is 6 . However, float f = 12345.6; and then printing it using printf() does not print 12345.6 , it prints 12345.599609 . So what does "6 significant digits" (or "15 in case of a double ") mean for a floating point type? 回答1: According to the standard, not all decimal number can be stored exactly in memory. Depending on the size of the representation, the error can get to a certain maximum. For float this is 0

Why does str(float) return more digits in Python 3 than Python 2?

会有一股神秘感。 提交于 2019-11-26 19:06:13
In Python 2.7, repr of a float returns the nearest decimal number up to 17 digits long; this is precise enough to uniquely identify each possible IEEE floating point value. str of a float worked similarly, except that it limited the result to 12 digits; for most purposes this is a more reasonable result, and insulates you from the slight differences between binary and decimal representation. Python 2 demo: http://ideone.com/OKJtxv print str(1.4*1.5) 2.1 print repr(1.4*1.5) 2.0999999999999996 In Python 3.2 it appears str and repr return the same thing. Python 3 demo: http://ideone.com/oAKRsb

How to convert a float into hex

前提是你 提交于 2019-11-26 17:41:38
问题 In Python I need to convert a bunch of floats into hexadecimal. It needs to be zero padded (for instance, 0x00000010 instead of 0x10). Just like http://gregstoll.dyndns.org/~gregstoll/floattohex/ does. (sadly i can't use external libs on my platform so i can't use the one provided on that website) What is the most efficient way of doing this? 回答1: This is a bit tricky in python, because aren't looking to convert the floating-point value to a (hex) integer. Instead, you're trying to interpret

C floating point precision [duplicate]

狂风中的少年 提交于 2019-11-26 16:48:19
问题 Possible Duplicate: Floating point comparison I have a problem about the accuracy of float in C/C++. When I execute the program below: #include <stdio.h> int main (void) { float a = 101.1; double b = 101.1; printf ("a: %f\n", a); printf ("b: %lf\n", b); return 0; } Result: a: 101.099998 b: 101.100000 I believe float should have 32-bit so should be enough to store 101.1 Why? 回答1: You can only represent numbers exactly in IEEE754 (at least for the single and double precision binary formats) if

problems in floating point comparison [duplicate]

ぃ、小莉子 提交于 2019-11-26 15:33:30
This question already has an answer here: strange output in comparison of float with float literal 8 answers void main() { float f = 0.98; if(f <= 0.98) printf("hi"); else printf("hello"); getch(); } I am getting this problem here.On using different floating point values of f i am getting different results. Why this is happening? f is using float precision, but 0.98 is in double precision by default, so the statement f <= 0.98 is compared using double precision. The f is therefore converted to a double in the comparison, but may make the result slightly larger than 0.98. Use if(f <= 0.98f) or

Why does str(float) return more digits in Python 3 than Python 2?

≯℡__Kan透↙ 提交于 2019-11-26 04:52:37
问题 In Python 2.7, repr of a float returns the nearest decimal number up to 17 digits long; this is precise enough to uniquely identify each possible IEEE floating point value. str of a float worked similarly, except that it limited the result to 12 digits; for most purposes this is a more reasonable result, and insulates you from the slight differences between binary and decimal representation. Python 2 demo: http://ideone.com/OKJtxv print str(1.4*1.5) 2.1 print repr(1.4*1.5) 2.0999999999999996

problems in floating point comparison [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-11-26 04:28:31
问题 This question already has an answer here: strange output in comparison of float with float literal 8 answers void main() { float f = 0.98; if(f <= 0.98) printf(\"hi\"); else printf(\"hello\"); getch(); } I am getting this problem here.On using different floating point values of f i am getting different results. Why this is happening? 回答1: f is using float precision, but 0.98 is in double precision by default, so the statement f <= 0.98 is compared using double precision. The f is therefore