Swift: NSNumber is not a subtype of Float - swift

I tried the code from Swift Programming Language in playground and got the following error "NSNumber is not a subtype of Float", I just modified it slightly by making x and y of type Float in struct Point. What am I missing?
If I added Float type to centerX and centerY, I got error: Could not find an overload for '/' that accepts the supplied arguments.

The error message is completely unrelated to the actual error... The actual error is cannot convert Double to Float.
In Size, x and y are Double (default type of float literal) but in Point, width and height are Float. They are different types and you can't mix them without explicit conversion.
There are number of ways to fix it. You can change them all to Double or Float.
e.g.
class Point
{
var x:Double
var y:Double
}
or you can convert them to correct type by doing Float(centerX)
ps: can you post the code next time so I can change it without retype them

Related

Implications of automatic casting with double on dart/flutter

What are the implications of using an int were a double is required? dart allows you to declare a double without the .0 if it is 0 and handle that as a double without any kind of runtime casting?
Thinks that makes me worry about this:
I dont see any linter saying that this will cast the int into a double.
It compiles fine.
The height field is a double but it accepts an int.
Take a look to the examples:
int version
SizedBox(height: 10)
# Or
final double a = 2;
double version
SizedBox(height: 10.0)
# Or
final double a = 2.0;
If you look at the SizedBox implementation you'll see that the field height has the type double.
To understand what happens in your code that you have as example you have to understand how type inference works. In the first code example that you gave (int version), even if you wrote 10 and not 10.0 the Dart compiler infer that value as a double because the filed height is of that type. You do not specify explicitly that the value that you give as parameter is int so it's seen as double. If you give as value an integer (you specify the type int) you'll have the next compile-time error:
So, to conclude this, in both of your examples Dart infer the type as double because you don't say explicitly that is an integer.
You can read more about Dart type inference here: https://dart.dev/guides/language/type-system#type-inference
There is no automatic runtime conversion between int and double. double a = 2; is simply syntactic sugar for double a = 2.0; which happens at compile time and which is why it works only for integer literals.

Implicit type of constant in swift tutorial

When I do example from tutorial, I get some issue from constants variables topic.
If someone explain my example I'll be appreciate for this.
When you don't specify a type, a floating point number literal will be inferred to be of type Double.
Double, as its name suggests, has double precision than Float. So when you do:
let a = 64.1
The actual value in memory may be something like 64.099999999999991. Since Double shows only 16 significant digits, it shows 64.09999999999999, rounding off the last "1".
Why does let b: Float = 64.1 show the correct number?
When you specify the type to float, the precision decreases. Float only shows 8 significant digits. That's 64.099999, but there's a "9" straight after that, so it rounds it up to get 64.1.
This has nothing to do with explicitly stating the variable type. Try specifying it to be a Double:
let b: Double = 64.1
It will still show 64.09999999999999.

Swift-Binary operator cannot be applied to operands, when converting degrees to radians

I'm aware of some relatively similar questions on this site, but if they do apply to my problem (which I'm not certain they do) then I certainly don't understand them. Here's my problem;
var degrees = UInt32()
var radians = Double()
let degrees:UInt32 = arc4random_uniform(360)
let radians = angle * (M_PI / 180)
This returns an error, focused on the multiplication star, reading; "Binary operator "*" cannot be applied to operands of type 'UInt32' and 'Double'.
I'm fairly sure I need to have the degrees variable be of type UInt32 to randomise it, and also that the pi constant cannot be made to be of UInt32, or at least I don't know how, as I'm relatively new to Xcode and Swift in general.
I'd be very grateful if anyone had a solution to my problem.
Thanks in advance.
let degree = arc4random_uniform(360)
let radian = Double(degree) * .pi/180
you need to convert the degree to double before the multiplication .
from apple swift book:
Integer and Floating-Point Conversion
Conversions between integer and floating-point numeric types must be made explicit:
let three = 3
let pointOneFourOneFiveNine = 0.14159
let pi = Double(three) + pointOneFourOneFiveNine
// pi equals 3.14159, and is inferred to be of type Double
Here, the value of the constant three is used to create a new value of type Double, so that both sides of
the addition are of the same type. Without this conversion in place, the addition would not be allowed.
Floating-point to integer conversion must also be made explicit. An integer type can be initialized
with a Double or Float value:
1 let integerPi = Int(pi)
2 // integerPi equals 3, and is inferred to be of type Int
Floating-point values are always truncated when used to initialize a new integer value in this way.
This means that 4.75 becomes 4, and -3.9 becomes -3.

Read a double * in Swift

I have an Obj-C method that returns a double *, how is this accessed in Swift as a Double?
I get this error
Cannot convert value of type 'UnsafeMutablePointer<Double>' to expected argument type 'Double'
I am calling this - (double * _Nonnull) modIntensityForDestination:(int) destination;
and failing when I do this
let intensity = audioEngine.modIntensityForDestination(Int32(modDestinationID))
I have tried withUnsafeMutablePointer but cant seem to get it working.
If the method returns a pointer to a single floating point number
then you can dereference it with .memory:
let intensity = audioEngine.modIntensityForDestination(...).memory
In Swift 3 it would be .pointee.

Attempting to have SKLabelNode show a float

Trying to get a SKLabelNode to show a float, but instead it only shows an integer. This is what i have at the moment:
sdrLabel.text = String(Float(NSUserDefaults.standardUserDefaults().integerForKey("TotalScore") / NSUserDefaults.standardUserDefaults().integerForKey("TotalDeath")))
I tried to type cast it to a float and present it as a string, but it still shows an int. For example 233 / 8, it shows 29.
You are loosing precision because you are working with integers and then you cast the result to Float. You need to work with Float (or Double) when dividing those two numbers.
You can use this instead:
sdrLabel.text = String(NSUserDefaults.standardUserDefaults().doubleForKey("TotalScore") / NSUserDefaults.standardUserDefaults().doubleForKey("TotalDeath"))