Swift: Print decimal precision of division - swift

I'm trying print the result of division for example:
let division = (4/6)
print(division)
In this case the print out is 0.
How can I print the numeric value of the division without losing the numeric value. I mean without casting the output to string.

You are performing integer division. You need to perform floating point division.
In your code, division is an Int value. 4 / 6 is zero in integer division.
You need:
let division = 4.0/6.0
print(division)

ans = Double(no1)/Double(no2)
return ans

If you want the value should be correct, then try as
let division = Float(v1) / Float(v2)
print(division)

Related

Swift, reducing numeric error using Decimal type

I found that making operand in Decimal can reduce numeric error in Swift
0.1 + 0.2 = 0.30000000000000004
Decimal(0.1) + Decimal(0.2) = 0.3
So I tried to make a function that calculate calculation String in Decimal like this:
func calculate(expression: String) -> Decimal {
let expression = NSExpression(format: expression)
let value = expression.expressionValue(with: nil, context: nil) as? Decimal
return value ?? 0.0
}
But value property keep getting nil value and function always returning 0.0. Can I get some any help on this?
Thanks
The Decimal type does not reduce numeric error. It just computes values in decimal. That can increase or decrease error, depending on the calculation. 1/10 happens to be bad in binary for the same reason that 1/3 is bad in decimal. Your code doesn't actually compute anything in Decimal. It's just trying to convert the final Double value to Decimal at the end, which introduces binary-to-decimal rounding (making it less accurate).
That said, expressionValue(with:context:) returns an NSNumber. You can't convert that to Decimal with as?. You need to use .decimalValue:
let number = expression.expressionValue(with: nil, context: nil) as? NSNumber
let value = number?.decimalValue
This will compute the value in Double and then round it to a Decimal.
But if you want to do calculations in Decimal, I don't believe that NSExpression can do that.

How I can get the other part after a division with a modulo operator

When I divide 13 with 3 and use integer numbers the result will be 4.
With mod(13,3) I receive the remainder 1. But how can I get the 4 in Matlab? I think it is not possible to switch to integer numbers for this calculation, isn't it?
You can use the floor function:
result = floor(13/3)
This function always rounds down to the lower integer
You can explicitly use integers:
result = uint32(13)/unit32(3);
You can also use hex numbers:
result = 0xDu32 / 0x3u32;
Note that result will be of type uint32.
Use idivide:
result = idivide(13, 3);
You can specify the rounding method with a third argument, with the default being 'fix', or rounding towards zero. For example, this would round towards negative infinity:
result = idivide(13, 3, 'floor');

Dividing UInt64 always returns 0 [duplicate]

I'm trying print the result of division for example:
let division = (4/6)
print(division)
In this case the print out is 0.
How can I print the numeric value of the division without losing the numeric value. I mean without casting the output to string.
You are performing integer division. You need to perform floating point division.
In your code, division is an Int value. 4 / 6 is zero in integer division.
You need:
let division = 4.0/6.0
print(division)
ans = Double(no1)/Double(no2)
return ans
If you want the value should be correct, then try as
let division = Float(v1) / Float(v2)
print(division)

Decimal Value Set as 0 in Swift

I am trying to get a random decimal from 0.75 to 1.25
let incomeCalc = Decimal((arc4random_uniform(50)+75)/100)
print("incomeCalc")
print(incomeCalc)
Why does this print 0?
arc4random_uniform return an integer type so you are doing integer math. You need to be doing floating point math.
let incomeCalc = Decimal(Double((arc4random_uniform(50)+75))/100)
By casting the value before you do the division, you get a Double result which is passed to your Decimal initializer.
Or you can do:
let incomeCalc = Decimal((arc4random_uniform(50)+75))/100
which creates the Decimal before the division is done.
You can also use the code below which gets a random number between 75 - 125 and then divides it by 100
let incomeCalc = Decimal((arc4random_uniform(50)+75)) / 100
print("incomeCalc")
print(incomeCalc)

Exponent value not displaying,what should i do?

how to display exponent value after calulation to textbox in iphone sdk.For example say 6.4516e-10.i am not getting answer for it in my textbox after calculating 10 * 6.4516e-10.please tell me solution..
Use StringWithFormat and exponent formations:
From the man page on printf:
eE The argument is printed in the style e `[-d.ddd+-dd]' where is one digit before the decimal point and the number after is equal to the precision specification for the argument; when the precision is missing, 6 digits are produced.
So you would want a format something like: %.4e
float n = 6.4516e-10;
n = n * 10;
NSLog(#"n: %.4e", n);
2011-08-29 07:36:38.158 Test[39477:707] n: 6.4516e-09