Calculate Double accurately - swift

How to calculate doubles accurately in Swift?
I try it on Swift playground, and 10.0 - 0.2 won't be 9.8.
How to fix it?

You could use round.
So for your example this should work:
let x = 10.0 - 0.2
let y = Double(round(100*x)/100)
If you want more accuracy multiply and divide for example by 1000 or 10000 and so on.

Related

How to round up and down double to the nearest interval in dart/flutter

I'm trying to round down/up for example i want to round down 134.78 with nearest 0.5 the output should be 134.5 and if i round up the output should be 135.0 I tried ((134.78 / 0.5).floorToDouble() * 0.5) but the result is 134.0 not 134.5
Multiply your number by two before any calculations.
Use the desired round feature. (Floor or Ceiling).
Divide the result by two again:
double a = 134.78;
print((2*a).floorToDouble()/2); // prints 134.5
print((2*a).ceilToDouble()/2);// prints 135
Which is the desired result.
You can read more here.

Round up to the nearest .5 decimal in swift

I have a function that looks like this
bits = bits * 1.5
So if bits was 1 it would return 1.5 but if the function was run again it would return 2.25 however i would prefer if it were to just round up to 2.5 is that possible?
Maybe try the following function from globalnerdy.com:
In your case you could use bits = roundUp(bits, 0.5) after the multiplication by 1.5.
func roundUp(_ value: Double, toNearest: Double) -> Double {
return ceil(value / toNearest) * toNearest
}
You could do it by doing...
round(x * 3) * 0.5
So multiply by 3. Then round it to the nearest int. Then divide by 2.
So you have still multiplied by 1.5 but it gets rounded to the nearest 0.5
1.5 * 3 = 4.5
Rounded = 5
5 / 2 = 2.5
Which is what you wanted
Edit: if you want to always go up to the nearest 0.5 then use ceiling instead of round.

How would you round up or down a Float to nearest even numbered integer in Swift 3?

I need a little help rounding up or down a float to the nearest even number in Swift.
Eg:
32.86 would be closest to 32
33.86 would be closest to 34
If you want to round to the nearest even number, divide by 2, round and then multiply by 2:
let rounded = Int(round(value / 2.0)) * 2
What about this using Swift 4?
33.86.rounded(.toNearestOrEven)

How to floor a floating point number? from 0.999 to 0.99

I want to truncate any value that appears after two decimal places in Matlab/Octave.
I do not want the value 1. I instead want the value to be 0.99 after "rounding/flooring".
Please use only built-in functions to accomplish this task.
Scale up the number, apply rounding, scale the result down by the same value:
x = 0.999;
y = floor (100 * x) / 100;

Loss of precision when dividing doubles in swift

I try to find the steps between a min and a max value with a given step-size, using swift 2.1.
So we have a min and a max value, both of type Double. The step-size is a Double too. If min is 0.0 and max 0.5 with steps of 0.1, the result is 6, obviously.
But if I start with -0.1 as the minimum value, the result is 6 too. But should be 7, agree?
Here is my Playground example:
let min:Double = -0.1
let max:Double = 0.5
let step:Double = 0.1
var steps: Int {
return Int((max - min) / step) + 1
}
print("steps: \(steps)") // returns "steps: 6", but should be 7
The result is 6.99999999 if we use a Double for the steps variable. But this loss of precision only occurs when our min value is negative.
Do you know a workaround? I just don't want to round() each time I calculate with Doubles.
When you use Int() it forces truncation of your number, which always rounds towards zero. So, 6.9999 becomes 6 rather than 7, because it's closer to zero. If you use round() first it should help:
var steps: Int {
return Int(round((max - min) / step) + 1.0)
}
That's always not a good idea to calculate integral steps based on floating point ranges, you'll always encounter issues, and you won't be able to do much.
Instead I recommend to build your logic on integral steps, and calculate double values based on integral values (not vice versa as you do). I.e. you don't calculate integral step based on range, but you set your integral number of steps and calculate your double step.