Getting the decimal part of a double in Swift

You can use truncatingRemainder and 1 as the divider. Returns the remainder of this value divided by the given value using truncating division. Apple doc Example: let myDouble1: Double = 12.25 let myDouble2: Double = 12.5 let myDouble3: Double = 12.75 let remainder1 = myDouble1.truncatingRemainder(dividingBy: 1) let remainder2 = myDouble2.truncatingRemainder(dividingBy: 1) let remainder3 = myDouble3.truncatingRemainder(dividingBy: …

Read more

Why float.Epsilon and not zero?

Actually, using float.Epsilon may not make any significant difference here. float.Epsilon is the smallest possible float greater than zero (roughly 1.401298E-45), which does not mean that it’s the smallest difference between any two arbitrary floats. Since floating-point math is imprecise, the difference between two seemingly equal numbers can be much greater than float.Epsilon. For example: …

Read more

How to get bc to handle numbers in scientific (aka exponential) notation?

Unfortunately, bc doesn’t support scientific notation. However, it can be translated into a format that bc can handle, using extended regex as per POSIX in sed: sed -E ‘s/([+-]?[0-9.]+)[eE]\+?(-?)([0-9]+)/(\1*10^\2\3)/g’ <<<“$value” you can replace the “e” (or “e+”, if the exponent is positive) with “*10^”, which bc will promptly understand. This works even if the exponent …

Read more

What is the standard solution in JavaScript for handling big numbers (BigNum)?

Update(2019-08-19): BigInt is now part of Firefox and Chrome; you no longer need a library: const bigInt1 = 1111111111111111111111111111111n; const bigInt2 = BigInt(“1111111111111111111111111111111″) console.log((bigInt1 + bigInt2)+””) Original answer: If you need arbitrary-precision decimal numbers, use Javascript-bignum, as it is correct and fast.