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.
Related
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.
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 5 years ago.
var num1 = 1.1
if num1 + 0.1 == 1.2 {print("true")} else {print("false")}
Result: false
var num1: Float = 1.1
if num1 + 0.1 == 1.2 {print("true")} else {print("false")}
Result: true
The former block of code has num1 which represents a double and the latter code block has num1 which represents a float.
My question: Why does the code with Float return true and the one with Double does not?
This is a duplicate and I will close it as such, but here is an example of why this works for Float but not for Double.
Instead of looking at Double and Float, let's look at two new types. Eighths and Sixteenths. Eighths has 3 bits to represent fractional numbers, so you can represent 0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, and 0.875. Sixteenths has 4 bits to represent fractional numbers, so you can represent 0, 0.0625, 0.125, 0.1875, 0.25, and so on. In both number types, it will be necessary to approximate floating point values by choosing the closest value to the one you wish to represent.
So lets look at what 1.1 + 0.1 looks like in Eighths and Sixteenths.
Eighths
1.1 = 1.125
0.1 = 0.125
1.2 = 1.25
1.1 + 0.1 = 1.125 + 0.125 = 1.25
so 1.1 + 0.1 == 1.2 in Eighths
Sixteenths
1.1 = 1.125
0.1 = 0.125
1.2 = 1.1875
1.1 + 0.1 = 1.125 + 0.125 = 1.25
so 1.1 + 0.1 != 1.2 in Sixteenths.
The greater precision of Sixteenths makes it possible to more accurately represent 1.2 with a smaller value when represented in Sixteenths.
This is what is happening with Floats and Doubles. There are more bits available to represent the numbers, but they are still approximations. When you do math with those approximations, the error combines in unexpected ways which is why it is ill advised to be using equals comparisons with floating point values.
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;
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.
I would like to round a Double to a certain number of decimals always rounding down.
Example
rounding to .00 => 1.5679999 to 1.56
rounding to .000 => 1.5679999 to 1.567
round(1.5679999 * 100) / 100 //returns 1.57
round(1.5679999 * 1000) / 1000 //returns 1.568
I want a Double not a concatenated String
Just replace round with floor!
As of swift 3.0:
myDouble.rounded(.down)
SWIFT4+
ways to round on different cases
var w = 6.5
w.round(.toNearestOrAwayFromZero)
// w == 7.0
// Equivalent to the C 'trunc' function:
var x = 6.5
x.round(.towardZero)
// x == 6.0
// Equivalent to the C 'ceil' function:
var y = 6.5
y.round(.up)
// y == 7.0
// Equivalent to the C 'floor' function:
var z = 6.5
z.round(.down)
// z == 6.0