Issue with Swift Floats inside Structs

swift float precision
swift float vs double
swift double
swift round float
swift float literal
swift data types
swift decimal
swift 4 round float to 2 decimal places

I'm having an issue where swift is changing my float value when it's inside a struct.. I dont think this happened on older versions of swift, but it is happening now on 4.1. Code example below... thoughts?

var f = Float(4.111)

print(f) // prints 4.111

struct FloatContainer {
    var f: Float

let container = FloatContainer(f: f)

print(container) // prints FloatContainer(f: 4.11100006)

That's a common rounding error, since binary can't represent some decimal numbers. Have a look here.

Here is an online converter from a decimal to a floating-point representation. As you can see there is an error (6.103515625E-8) due to the conversion:

As to the difference between when you print a float directly, or when you print an object that has a property that is a float, it comes down to the description property of objects that implement the CustomStringConvertible protocol. When the print function wants to print to the console, it invokes _print_unlocked, which is an internal function that has this block:

if case let printableObject as CustomStringConvertible = value {
    printableObject.description.write(to: &target)

So you could change your code to this in order to have the expected output:

struct FloatContainer : CustomStringConvertible {
    var description: String {
        get {
            return f.description
    var f: Float

Float, These initializers result in nil if the value passed can't be represented without static func random<T>(in: Range<Float>, using: inout T) -> Float struct Float. Declaring function as mutating inside struct allows us to alter properties in Structures. Swift Struct Initialisers. Default initializer takes 2 forms. Either an empty initializer() or the memberwise initializer that lists the structure’s properties inside its parenthesis so that you can assign new values to them.

You should try printing the value inside

print(container.f) // prints 4.111

This is returning what you are storing.

Everyday Data Structures, initializing 146 initializing, in C# 146 initializing, in Java 146 initializing, in floating point about 16 in C# 17 in Java 17 in Objective-C 18 in Swift 18 singly linked  A series of Swift Tutorials that will cover all the basics of Apple's latest programming language - Swift 4! In part 12 we'll learn about structs in swift and how to create/use them. Get my Apple

Swift provides default debug representation for any type and this is the reason Apple documentation

 var f = Float(4.111)

    print(f) // prints 4.111

    print(f.debugDescription) // prints 4.11100006
    print(String(reflecting: f)) // default presentation according to documentation: prints 4.11100006

    struct FloatContainer: CustomStringConvertible {
        var f: Float

        // You can achieve similar output to `print(f)` with conforming
        // to `CustomStringConvertible` and implementing custom 
        // `description` implementation
        var description: String {
            return f.description

    let container = FloatContainer(f: f)
    // After our custom implementation the result of `print` 
    // method is the same to print(f)
    print(container) // prints FloatContainer(f: 4.111)

White Whole, When the last angel of the broken world finds me floating in the black whole of light, through connections with swift electron coils into plants, insects, animals, and All things that exist in our universe are structures of atoms pulsing with light  Swift makes it easy to create arrays in your code using an array literal: simply surround a comma-separated list of values with square brackets. Without any other information, Swift creates an array that includes the specified values, automatically inferring the array’s Element type.

IOS 9 Programming Fundamentals with Swift: Swift, Xcode, and Cocoa , let n = NSNumber(float:0) Coming back from Objective-C to Swift, a value will The problem being solved here is parallel to the problem solved by NSNumber: a Swift struct is an object, but a C struct is not, so a struct cannot be used in  But I still do not understand why does scanf have problems with a float variable inside a structure but not with a normal float variable. Thanks anyway for the suggestions. I will certainly try and get a better compiler once I am through with my lab test.-Sreejit

Park and Recreation Structures, There are water-front situations in which a sharply sloping beach, mucky bottom, swift current, or other The only satisfactory location for diving boards is a fixed structure. The behavior of a float in rough weather is unpredictable, and painful​  Best Usage Practices of Structures. Swift 4 language provides the functionality to define structures as custom data types for building the function blocks. The instances of structure are passed by its value to the defined blocks for further manipulations. Need for having structures. To encapsulate simple data values.

Smart Materials and Structures: Proceedings of the 4th European , Nature's paradigms The swift is a bird and is a typical product of organic evolution. The plants, (single cells floating in the water) get their energy from sunlight that organisms are bags of answers to the questions and problems of survival. Use structs if you want value types. You can add functionality to both (and to enumerations) in Swift. When in doubt, err on the side of value types. If your construct doesn’t have a life cycle, or two constructs with similar values should be considered as the same thing, use a struct.

  • Related:…
  • Possible duplicate of Is floating point math broken?
  • Also related: – floating point values inside containers are (by default) printed with a higher precision.
  • I've updated my answer, I hope it helps.
  • That does not yet explain why the same value f is printed as 4.111 in one place, and as 4.11100006 in another place.
  • Still doesn’t explain why the value is different when referencing the struct vs the actual value.