How to find max value for Double and Float in Swift
As of Swift 3+, you should use: CGFloat.greatestFiniteMagnitude Double.greatestFiniteMagnitude Float.greatestFiniteMagnitude
As of Swift 3+, you should use: CGFloat.greatestFiniteMagnitude Double.greatestFiniteMagnitude Float.greatestFiniteMagnitude
Your declaration of a float contains two parts: It declares that the variable timeRemaining is of type float. It assigns the value 0.58 to this variable. The problem occurs in part 2. The right-hand side is evaluated on its own. According to the C# specification, a number containing a decimal point that doesn’t have a … Read more
You don’t need to convert it at all: % perl -e ‘print “5.45” + 0.1;’ 5.55
Since C++11, you may use nextafter to get next representable value in given direction: std::nextafter(1., 0.); // 0.99999999999999989 std::nextafter(1., 2.); // 1.0000000000000002 Demo
Floating point division by zero is well defined by IEEE and gives infinity (either positive or negative according to the value of the numerator (or NaN for ±0) ). For integers, there is no way to represent infinity and the language defines the operation to have undefined behaviour so the compiler helpfully tries to steer … Read more
I’d say the answer depends on the rounding mode when converting the double to float. float has 24 binary bits of precision, and double has 53. In binary, 0.1 is: 0.1₁₀ = 0.0001100110011001100110011001100110011001100110011…₂ ^ ^ ^ ^ 1 10 20 24 So if we round up at the 24th digit, we’ll get 0.1₁₀ ~ 0.000110011001100110011001101 … Read more
Because… many programs don’t use floating point or don’t use it on any given time slice; and saving the FPU registers and other FPU state takes time; therefore …an OS kernel may simply turn the FPU off. Presto, no state to save and restore, and therefore faster context-switching. (This is what mode meant, it just … Read more
So, basically, you want your code to run faster. JNI is the answer. I know you said it didn’t work for you, but let me show you that you are wrong. Here’s Dot.java: import java.nio.FloatBuffer; import org.bytedeco.javacpp.*; import org.bytedeco.javacpp.annotation.*; @Platform(include = “Dot.h”, compiler = “fastfpu”) public class Dot { static { Loader.load(); } static float[] … Read more
Java doubles are in IEEE-754 format, therefore they have a 52-bit fraction; between any two adjacent powers of two (inclusive of one and exclusive of the next one), there will therefore be 2 to the 52th power different doubles (i.e., 4503599627370496 of them). For example, that’s the number of distinct doubles between 0.5 included and … Read more
For a general-purpose¹ solution you need to preserve 339 places: doubleValue.ToString(“0.” + new string(‘#’, 339)) The maximum number of non-zero decimal digits is 16. 15 are on the right side of the decimal point. The exponent can move those 15 digits a maximum of 324 places to the right. (See the range and precision.) It … Read more