Dividing two doubles gives wrong result in Xcode console [duplicate] - ios

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
I was trying some things in Xcode, and ran into an unexplained situation where Xcode gives me a wrong result for a simple division:
let a : Double = 0.235
let b : Double = 0.001
let nrOfDivisions = a / b
print("Divisions: ", nrOfDivisions) //prints 234.99999999999997
Strange enough, if I divide from 0.230 ... 0.234 to the same amount of 0.001, I get correct results, but starting from 0.235 ... 0.239 I get these wrong results.
I've tested now with 0.225, 0.226, 0.227, 0.245, 0.246, 0.247 and they all divide correctly.
What might be the issue here? It is a bug in Xcode, or am I missing something?

Well this is probably due to this issue: Why not use Double or Float to represent currency?. Were you thinking that Apple implemented floating point wrong? In the Java World, these questions came up quite often, and BigDecimal was the solution, you can read about that.

Related

Check if integer divided by 2. SWIFT [duplicate]

This question already has answers here:
How to know if a number is odd or even in Swift?
(5 answers)
Closed 11 months ago.
im absolutely new to development, and trying to learn swift.
Right now i know how to make random number, and my next step is:
Im trying to understand how to check if my random number (127) could be divided by 2 without decimals ?
I have no idea how to do it.
There is a specific API isMultiple(of:) in Standard Library for this purpose
let random = Int.random(in: 0..<100)
let isEven = random.isMultiple(of: 2)
You can use operator % - remainder operator
example:
if randomNumber % 2 == 0 {
print("\(randomNumber) is even")
} else {
print("\(randomNumber) is odd")
}

Creating an array with operands results in nil members

I just started learning lua and run into a strange problem. The following code...
local xx = 100
vertices0 = {xx, xx}
vertices1 = {xx−5, xx-5}
results in...
an array containing (100,100) for vertices0 (as expected) but
in an array containing (nil, 95)for vertices1.
I really dont understand what is causing the nil to appear. I expected to get an array with (95,95).
I checked the documentation and tried to google the problem. But was not able to solve this problem.
Btw - I'm using love2d but "regular" lua seeems to cause the same behaviour.
xx−5 is not using - but −, lua treats − as part of a identifier so xx−5 is a separate identifier rather than the desired subtraction operation xx - 5
local xx = 100
local xx−5 = 100
vertices0 = {xx, xx}
vertices1 = {xx−5, xx-5}
print(vertices1[1])
This appears to work in 5.1, but not later version of lua. additionally an issue like this can be seen easier if you place a space around an operator and it's operands, which does tend to be a good style choice for readability.
vertices1 = {xx − 5, xx - 5}
Also if you have syntax highlighting than you can notice the improper char does not get highlighted properly.

Best way to calculate the square root of any number in ios , Objective C and Swift

I am asking for ways to calculate the square root of any given number in ios, Objective C. I have inserted my way to do it using log. the logic was. ex : find the square root of 5
X = √5
then
log10X = log10(√5)
this means
log10X = log10(5)/2;
then should get the value of log10(5) and divide it from 2 and after that shoud get the antilog of that value to search X.
so my answer is in Objective C is like below (as an ex: I'm searching the square root of 5)
double getlogvalue = log10(5)/2; // in here the get the value of 5 in log10 and divide it from two.
//then get the antilog value for the getlogvalue
double getangilogvalue = pow(10,getlogvalue);
//this will give the square root of any number. and the answer may include for few decimal points. so to print with two decimal point,
NSLog(#"square root of the given number is : %.02f", getantilogvalue);
If anyone have any other way/answers. to get the square root of any given value , add please add and also suggestions for above answer is also accepted.
This is open for swift developers too. please add there answers also, becasue this will help to anyone who want to calculate the square root of any given number.
The sqrt function (and other mathematical functions as well) is
available in the standard libraries on all OS X and iOS platforms.
It can be used from (Objective-)C:
#include "math.h"
double sqrtFive = sqrt(5.0);
and from Swift:
import Darwin // or Foundation, Cocoa, UIKit, ...
let sqrtFive = sqrt(5.0)
In Swift 3,
x = 4.0
y = x.squareRoot()
since the FloatingPoint protocol has a squareRoot method and both Float and Double comply with the FloatingPoint protocol. This should be higher performance than the sqrt() function from either Darwin or Glibc, since the code it generates will be from the LLVM built-in square root, so no function call overhead on systems with hardware square root machine code.

How do I convert a floating point number into erlang time format (and vice versa)?

I am trying to convert an erlang time format tuple, {megasec,sec,microsec}, into a floating point number and back again.
I can do this one way, e.g.:
{Megasec,Sec,Usec} = erlang:now().
Total = Megasec*1000000+Sec+Usec/1000000.
1352802601.427
But I am struggling to convert this number back to the time format. I have a general idea to divide by 1000000 and round but I get rounding errors. e.g.
Mega = erlang:round(Total/1000000).
1353
If I could get this accurately I could apply similar steps to get Seconds and Microseconds.
Any ideas?
You can use erlang:trunc instead of erlang:round.
Following #Falco Hirschenberger's suggestion here's how I did it:
Mega = erlang:trunc(Total/1000000).
1352
Sec = erlang:trunc(Total - Mega*1000000).
802601
Usec = erlang:round((Total - Mega*1000000 - Sec)*1000000).
427000
Note. I had to use erlang:round to get Usec (else the answer would have been 427000.0457763672 - I think this is due to a rounding error introduced when I divided by 1000000)

How to check for NaN value in Objective-C (iOS) [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Objective-C - float checking for nan
Determine if NSNumber is NaN
I have a problem with NaN values in CGFloat, how can I check if the number is valid?
the only way so far that works is:
if ([[NSString stringWithFormat:#"%f", output] isEqualToString:#"nan"]) {
output = 0;
}
which is not a nice solution at all! :) ... and I am pretty sure that there is something else I should do instead.
There is a define for checking if a number is nan inf etc in math.h (you can use it without import I think).
isnan(myValue)
if you follow the define you will end up with
(x!=x)
there are also some other useful defines like isinf, isnormal , isfinite , ...

Resources