Why NSString floatvalue return 0 for "NaN"?

56 Views Asked by At

Why i dont get NaN as result? 0 is wrong. Maybe compiler settings?

CGFloat fValue = [@"NaN" floatValue]; // fValue is 0
double ddd = [@"NaN" doubleValue]; // also 0`

Edit: Strange, in swift this works and returns nan:

let fValue = Float ("NaN")
let ddd = Double ("NaN")
2

There are 2 best solutions below

4
son On

It's written on the documentation. This is for float value, the same with double value.

https://developer.apple.com/documentation/foundation/nsstring/1412321-floatvalue

This property doesn’t include whitespace at the beginning of the string. This property is HUGE_VAL or –HUGE_VAL on overflow, 0.0 on underflow. This property is 0.0 if the string doesn’t begin with a valid text representation of a floating-point number.

1
Hao Liang On

string "NaN" to float is 0.
0 is right and you is wrong.