how to compare if float variable value is infinite? [duplicate] - ios

This question already has an answer here:
Check for inf - Objective-C
(1 answer)
Closed 9 years ago.
debugger says me float value is inf
I just see in debug window section of xcode is this
floatVar1=(float)inf
inf means infinite ?
so how can I compare it ?
something like this:
if (floatVar1==INFITINE){
[self doBlah];
}

To test whether a value is either positive infinity or negative infinity:
if (isinf(floatVar1)) …
To test only whether a value is positive infinity:
if (floatVar1 == INFINITY) …
In either case, use #include <math.h>.

I guess this should work, though it's not objective-c put plain c:
if(isinf(floatVar1)) { ... }
Also, you need to include math.h. For more info see http://www.cplusplus.com/reference/cmath/isinf/

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")
}

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

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.

Round floating value to .1 (tens place) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I round a float value to 2 post decimal positions?
Lets say I have a double number of 3.46.
How do I round it to 3.50?
I tried
NSLog(#"res: %.f", round(3.46));
but it return 3.
Do some calculations....
float f=3.46;
float num=f+0.05;//3.51
int intNum=num*10;//35
float floatNum=intNum/10.0;//3.5
NSLog(#"res: %.2f", floatNum); //3.50
Following code may help
i = roundf(10 * i) / 10.0;
where i is your float variable
If you're willing to live with the rounding rules from printf, then the following should suffice when rounding for presentation:
NSLog(#"res: %.1f0", 3.46);
Note that the 0 is just a normal character that is added after the value is formatted to the appropriate number (1) of decimal places. This approach should be usable with [NSString stringWithFormat:] as well.
The original code results in "3" because round always returns an integral value.
YMMV; I don't even use iOS.

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 , ...

What does !!some_object do? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
What does !! mean in ruby?
what is this function doing?
def current_product?
!!current_product
end
Isn't that a double negative?
!! is basically a cast to boolean. If current_product is truthy, !current_product is false and !!current_product is true, and vice versa. I.e. it converts truthy values to true and falsy values to false.
It's effectively a cast/conversion to boolean.
Similar question, but for C++: Doube Negation in C++ code
Also a pretty decent post about it here: !! (The double bang / double not) in Ruby
This is a pattern you'll see in any language where every object has a truth value, but there are canonical booleans (whether they be called True and False, 1 and 0, 1 and "", t and nil, whatever). !!x is essentially a "cast to boolean", in that !!x has the same truth-value as x, but !!x will always be one of the canonical true/false values, instead of any old true/false value.

Resources