floating-point-conversion

std::pow with integer parameters, comparing to an integer type

你离开我真会死。 提交于 2019-11-28 13:51:06
According to http://en.cppreference.com/w/cpp/numeric/math/pow , when std::pow is used with integer parameters, the result is promoted to a double . My question is then the following: How safe is to compare an integer type with the result of a std::pow(int1, int2) ? For example, can the if below evaluate to true? std::size_t n = 1024; if(n != std::pow(2, 10)) cout << "Roundoff issues..." << endl; That is, is it possible that the result on the rhs can be something like 1023.99...9 so when converted to size_t becomes 1023? My guess is that the response in a big NO, but would like to know for

Converting Int to Float loses precision for large numbers in Swift

时间秒杀一切 提交于 2019-11-28 12:16:38
XCode 6.3.1 Swift 1.2 let value: Int = 220904525 let intmax = Int.max let float = Float(value) // Here is an error probably let intFromFloat = Int(float) let double = Double(value) println("intmax=\(intmax) value=\(value) float=\(float) intFromFloat=\(intFromFloat) double=\(double)") // intmax=9223372036854775807 value=220904525 float=2.20905e+08 intFromFloat=220904528 double=220904525.0 The initial value is 220904525. But when I convert it to float it becomes 220904528. Why? This is due to the way the floating-point format works. A Float is a 32-bit floating-point number, stored in the IEEE

Limit floating point precision?

末鹿安然 提交于 2019-11-28 11:10:34
Is there a way to round floating points to 2 points? E.g.: 3576.7675745342556 becomes 3576.76 . 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)); } If you are printing it out, instead use whatever print formatting function available to you. In c++ cout << setprecision(2) << f; For rounding to render to GUI, use std::ostringstream Multiply by 100, round to integer (anyway you want),

MySQL “greater than” condition sometimes returns row with equal value

青春壹個敷衍的年華 提交于 2019-11-28 07:56:49
问题 I'm running into a baffling issue with a basic MySQL query. This is my table: id | rating 1 | 1317.17 2 | 1280.59 3 | 995.12 4 | 973.88 Now, I'm attempting to find all rows where the rating column is larger than a certain value. If I try the following query: SELECT * FROM (`users`) WHERE `rating` > '995.12' It correctly returns 2 . But, if I try SELECT * FROM (`users`) WHERE `rating` > '973.88' it returns 4 ! So it's as if it thinks the 973.88 in the table is greater than 973.88, but it doesn

Random float number

邮差的信 提交于 2019-11-27 17:53:57
问题 I wrote this function to get a pseudo random float between 0 .. 1 inclusive: float randomFloat() { float r = (float)rand()/(float)RAND_MAX; return r; } However, it is always returning 0.563585. The same number no matter how many times I run my console application. EDIT: Here is my entire application if needed: #include <stdio.h> #include <stdlib.h> float randomFloat() { float r = (float)rand() / (float)RAND_MAX; return r; } int main(int argc, char *argv[]) { float x[] = { 0.72, 0.91, 0.46, 0

Why double can store bigger numbers than unsigned long long?

佐手、 提交于 2019-11-27 16:19:36
问题 The question is, I don't quite get why double can store bigger numbers than unsigned long long. Since both of them are 8 bytes long, so 64 bits. Where in unsigned long long, all 64 bits are used in order to store a value, on the other hand double has 1 for sign, 11 for exponent and 52 for mantissa. Even if 52 bits, which are used for mantissa, will be used in order to store decimal numbers without floating point, it still has 63 bits ... BUT LLONG_MAX is significantly smaller than DBL_MAX ...

C floating point precision [duplicate]

核能气质少年 提交于 2019-11-27 14:33:26
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? paxdiablo You can only represent numbers exactly in IEEE754 (at least for the single and double precision binary formats) if they can be constructed from adding together inverted powers of two (i.e., 2 -n like 1 , 1/2 , 1/4

Comparing float and double

我怕爱的太早我们不能终老 提交于 2019-11-27 09:36:17
#include <stdio.h> int main(void){ float a = 1.1; double b = 1.1; if(a == b){ printf("if block"); } else{ printf("else block"); } return 0; } Prints: else block #include <stdio.h> int main(void){ float a = 1.5; double b = 1.5; if(a == b){ printf("if block"); } else{ printf("else block"); } return 0; } Prints: if block What is the logic behind this? Compiler used: gcc-4.3.4 Mysticial This is because 1.1 is not exactly representable in binary floating-point. But 1.5 is. As a result, the float and double representations will hold slightly different values of 1.1 . Here is exactly the difference

std::pow with integer parameters, comparing to an integer type

我是研究僧i 提交于 2019-11-27 08:01:12
问题 According to http://en.cppreference.com/w/cpp/numeric/math/pow , when std::pow is used with integer parameters, the result is promoted to a double . My question is then the following: How safe is to compare an integer type with the result of a std::pow(int1, int2) ? For example, can the if below evaluate to true? std::size_t n = 1024; if(n != std::pow(2, 10)) cout << "Roundoff issues..." << endl; That is, is it possible that the result on the rhs can be something like 1023.99...9 so when

Converting Int to Float loses precision for large numbers in Swift

こ雲淡風輕ζ 提交于 2019-11-27 06:54:51
问题 XCode 6.3.1 Swift 1.2 let value: Int = 220904525 let intmax = Int.max let float = Float(value) // Here is an error probably let intFromFloat = Int(float) let double = Double(value) println("intmax=\(intmax) value=\(value) float=\(float) intFromFloat=\(intFromFloat) double=\(double)") // intmax=9223372036854775807 value=220904525 float=2.20905e+08 intFromFloat=220904528 double=220904525.0 The initial value is 220904525. But when I convert it to float it becomes 220904528. Why? 回答1: This is due