BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Measurement conversion error?

Forums > Swift

I've been playing with "Measurements" and noticed there's a little mismatch when converting a measurement:


import Foundation

var someMeasurement: Measurement<UnitMass> = Measurement(value: 0.5, unit: .milligrams)

print(someMeasurement.formatted())
print(someMeasurement.converted(to: .micrograms))

500 μg

499.99999999999994 µg

Am I doing something wrong?

1      

This is more likely a limitation computers have when representing floating-point values and doing floating-point math than an error in your code. A value you expect to be something like .5 ends up with a value like .4999999999999, and the printed value is slightly off.

1      

Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.