TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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?

3      

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.

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.