floating-point

C99 floating point intermediate results

戏子无情 提交于 2021-02-08 07:51:10
问题 As per the C99 standard: 6.3.1.8.2 : The values of floating operands and of the results of floating expressions may be represented in greater precision and range than that required by the type; the types are not changed thereby.52)> However, outside the scope of Annex F, we have: 5.2.4.2.2.7 : The values of operations with floating operands and values subject to the usual arithmetic conversions and of floating constants are evaluated to a format whose range and precision may be greater than

Converting floating point to unsigned int while preserving order

三世轮回 提交于 2021-02-08 03:01:50
问题 I have found a lot of answers on SO focusing on converting float to int . I am manipulating only positive floating point values. One simple method I have been using is this: unsigned int float2ui(float arg0) { float f = arg0; unsigned int r = *(unsigned int*)&f; return r; } The above code works well yet it fails to preserve the numeric order. By order I mean this: float f1 ...; float f2 ...; assert( ( (f1 >= f2) && (float2ui(f1) >= float2ui(f2)) ) || ( (f1 < f2) && (float2ui(f1) < vfloat2ui

Converting floating point to unsigned int while preserving order

天涯浪子 提交于 2021-02-08 02:59:02
问题 I have found a lot of answers on SO focusing on converting float to int . I am manipulating only positive floating point values. One simple method I have been using is this: unsigned int float2ui(float arg0) { float f = arg0; unsigned int r = *(unsigned int*)&f; return r; } The above code works well yet it fails to preserve the numeric order. By order I mean this: float f1 ...; float f2 ...; assert( ( (f1 >= f2) && (float2ui(f1) >= float2ui(f2)) ) || ( (f1 < f2) && (float2ui(f1) < vfloat2ui

Is n+Math.random() always smaller than n+1?

不想你离开。 提交于 2021-02-07 21:35:09
问题 I know Math.random() is smaller than 1, but the problem is, Math.random() generates floating point numbers, and floating point addition may have rounding errors. So my question is, is there any possible value of n which n+Math.random() < n+1 is false? 回答1: The largest random result is the largest double that is strictly less than 1.0. Adding one to it gets a real number result that is exactly half way between 2.0 and the largest double that is less than 2.0. Round to nearest will round it to

Delphi 6 Compiler Options (Pentium-safe FDIV)

元气小坏坏 提交于 2021-02-07 19:56:38
问题 I recieved a crash report from MadExcept from a user. The Exception was Invalid floating point operation. The odd part though is that the callstack dies at @FSafeDivide. I did a google and found out that this was a check for certain pentium chips which didn't do division correctly. If the test failed all the divisions would be done in software rather than hardware. I have the Pentium-Safe FDIV option turned on in my compiler settings. Could this have caused the error? I also read somewhere

Convert array of values into a single float value in PHP?

六眼飞鱼酱① 提交于 2021-02-07 19:49:49
问题 I have an array with these values (when the array is printed with print_r(); Array: [0] => 66 [1] => 233 [2] => 204 [3] => 205 The values in hex are: Array: [0] => 0x42 [1] => 0xE9 [2] => 0xCC [3] => 0xCD What I'm looking to do is to turn this 4 byte array into a float value. If I use implode(); to turn the array into a value, it just combines the string into 66233204205 instead of 0x42E9CCCD which are not similar. Thus I can't use floatval() . PHP is new to me, and so is using string values

When is a float variable not equal to itself

社会主义新天地 提交于 2021-02-07 14:37:39
问题 I was asked this questino on a programming test. The question was, I was passed in a float as a parameter, and asked when would the following code be false. bool result = (floatValue == floatValue); I couldn't think of a valid reason or a situation of when this would be false and still can't. In the end, I answered that there will never be a case when this would be false. Was wondering if anyone can give me some example(s) of when this would be false 回答1: floatValue == NaN If floatValue is

Returning From Catching A Floating Point Exception

 ̄綄美尐妖づ 提交于 2021-02-07 14:37:39
问题 So, I am trying to return from a floating point exception, but my code keeps looping instead. I can actually exit the process, but what I want to do is return and redo the calculation that causes the floating point error. The reason the FPE occurs is because I have a random number generator that generates coefficients for a polynomial. Using some LAPACK functions, I solve for the roots and do some other things. Somewhere in this math intensive chain, a floating point exception occurs. When

When is a float variable not equal to itself

蓝咒 提交于 2021-02-07 14:37:37
问题 I was asked this questino on a programming test. The question was, I was passed in a float as a parameter, and asked when would the following code be false. bool result = (floatValue == floatValue); I couldn't think of a valid reason or a situation of when this would be false and still can't. In the end, I answered that there will never be a case when this would be false. Was wondering if anyone can give me some example(s) of when this would be false 回答1: floatValue == NaN If floatValue is

When is a float variable not equal to itself

空扰寡人 提交于 2021-02-07 14:36:29
问题 I was asked this questino on a programming test. The question was, I was passed in a float as a parameter, and asked when would the following code be false. bool result = (floatValue == floatValue); I couldn't think of a valid reason or a situation of when this would be false and still can't. In the end, I answered that there will never be a case when this would be false. Was wondering if anyone can give me some example(s) of when this would be false 回答1: floatValue == NaN If floatValue is