floating-point

Implement ceil() in C

有些话、适合烂在心里 提交于 2021-01-27 15:11:23
问题 I want to implement my own ceil() in C . Searched through the libraries for source code & found here, but it seems pretty difficult to understand. I want clean & elegant code. I also searched on SO, found some answer here. None of the answer seems to be correct. One of the answer is: #define CEILING_POS(X) ((X-(int)(X)) > 0 ? (int)(X+1) : (int)(X)) #define CEILING_NEG(X) ((X-(int)(X)) < 0 ? (int)(X-1) : (int)(X)) #define CEILING(X) ( ((X) > 0) ? CEILING_POS(X) : CEILING_NEG(X) ) AFAIK, the

How should I obtain the fractional part of a floating-point value?

≯℡__Kan透↙ 提交于 2021-01-27 12:16:26
问题 I have a variable x of type float , and I need its fractional part. I know I can get it with x - floorf(x) , or fmodf(x, 1.0f) My questions: Is one of these always preferable to the other? Are they effectively the same? Is there a third alternative I might consider? Notes: If the answer depends on the processor I'm using, let's make it x86_64, and if you can elaborate about other processors that would be nice. Please make sure and refer to the behavior on negative values of x . I don't mind

int and float in function overloading

纵饮孤独 提交于 2021-01-27 11:43:58
问题 I have two overloaded function like below: void print(int i) { ... } void print(float f) { ... } Its giving me this error for print(1.2); : error: call of overloaded 'print(double)' is ambiguous Can anyone explain me why? 回答1: 1.2 is a double literal not a float. So the compiler requires an explicit disambiguation. 1.2f would work as that is a float literal. 回答2: 1.2 is a double literal, making the function you're trying to call ambiguous - a double can just as easily be truncated to a float

int and float in function overloading

心已入冬 提交于 2021-01-27 11:42:03
问题 I have two overloaded function like below: void print(int i) { ... } void print(float f) { ... } Its giving me this error for print(1.2); : error: call of overloaded 'print(double)' is ambiguous Can anyone explain me why? 回答1: 1.2 is a double literal not a float. So the compiler requires an explicit disambiguation. 1.2f would work as that is a float literal. 回答2: 1.2 is a double literal, making the function you're trying to call ambiguous - a double can just as easily be truncated to a float

Does std::scientific always result in normalized scientific notation for floating-point numbers?

 ̄綄美尐妖づ 提交于 2021-01-27 07:06:46
问题 Scientific notation defines how numbers should be displayed using a sign, a number and an exponent but it does not state that the visualization is normalized. An example: -2.34e-2 (normalized scientific notation) is the same as -0.234e-1 (scientific notation) Can I rely on the following code always producing the normalized outcome? Edit: except NAN and INF as pointed out in the answers. template<typename T> static std::string toScientificNotation(T number, unsigned significantDigits) { if

Does std::scientific always result in normalized scientific notation for floating-point numbers?

柔情痞子 提交于 2021-01-27 07:06:39
问题 Scientific notation defines how numbers should be displayed using a sign, a number and an exponent but it does not state that the visualization is normalized. An example: -2.34e-2 (normalized scientific notation) is the same as -0.234e-1 (scientific notation) Can I rely on the following code always producing the normalized outcome? Edit: except NAN and INF as pointed out in the answers. template<typename T> static std::string toScientificNotation(T number, unsigned significantDigits) { if

Operations on “double” and optimization in C

做~自己de王妃 提交于 2021-01-27 07:05:18
问题 I have recently analyzed an old piece of code compiled with VS2005 because of a different numerical behaviour in "debug" (no optimizations) and "release" (/O2 /Oi /Ot options) compilations. The (reduced) code looks like: void f(double x1, double y1, double x2, double y2) { double a1, a2, d; a1 = atan2(y1,x1); a2 = atan2(y2,x2); d = a1 - a2; if (d == 0.0) { // NOTE: I know that == on reals is "evil"! printf("EQUAL!\n"); } The function f is expected to print "EQUAL" if invoked with identical

Exhausting floating point precision in a (seemingly) infinite loop

本小妞迷上赌 提交于 2021-01-27 06:40:09
问题 I've got the following Python script: x = 300000000.0 while (x < x + x): x = x + x print "exec: " + str(x) print "terminated" + str(x) This seemingly infinite loop, terminates pretty quickly if x is a floating point number. But if i change x to 300000000 instead, it gets into an infinite loop (runs longer than a minute in my test). I think this is to do with the fact that it's exhausting the precision of a floating point number that can be represented in memory. Can someone provide a more

Exhausting floating point precision in a (seemingly) infinite loop

こ雲淡風輕ζ 提交于 2021-01-27 06:39:40
问题 I've got the following Python script: x = 300000000.0 while (x < x + x): x = x + x print "exec: " + str(x) print "terminated" + str(x) This seemingly infinite loop, terminates pretty quickly if x is a floating point number. But if i change x to 300000000 instead, it gets into an infinite loop (runs longer than a minute in my test). I think this is to do with the fact that it's exhausting the precision of a floating point number that can be represented in memory. Can someone provide a more

Reading floats from file with python

戏子无情 提交于 2021-01-27 06:33:51
问题 My input file has the form: 5.0, 1000.0, 100000000000000.0, 115.2712, 230.538, 345.796, 461.0408, 1.053E-09, 1.839E-09, 1.632E-10, 1.959E-12, 4.109, 3.683, 3.586, 3.650 where every number is essentially in a line. What I want to do is read all the floats, and then append only columns 7 through 10 to an array. Here's what I've written: T=[] with open("test.txt", "r") as file1: for line in file1.readlines(): f_list = [float(i) for i in line.split(",")] T.append(float(f_list[7])) T.append(float