Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Enums and Structs Structs and their Methods Struct Methods

Unneeded parentheses

Why do we need to do (self.amount * (percentage/100))? It can't be for math reasons because everyone knows (or at least should) that x * y / 100 = (x * (y / 100)). Is this because of Swift, because I don't get any errors in JS or Python. I even tried this in the playgrounds and got no error. x * y / 100 and (x * (y / 100)) both return 1.32 (for x=11.0 and y=12.0). But when I compare them using == I get false. I think this has something to do with tuples, but (for x=11.0 and y=12.0) does not show up in the results pane as a tuple.

let x = 11.0
let y = 12.0
let res1 = x * y / 100
let res2 = (x * (y / 100))
res1 == res2
struct.swift
struct Expense {
    var description: String
    var amount: Double = 0.0

    init (description: String) {
        self.description = description
    }

    // add the calculateTaxes method here
    // it should accept only one parameter named 'percentage' of type Double
    func calculateTaxes(percentage: Double) -> Double {
      return self.amount * percentage / 100
    } 


}

EDIT: Both solutions pass in the challenge

3 Answers

Not sure. I don't think it is to do with passing by reference or passing by values, these are values.

It seems that forcing the order of the calculation with/without parentheses changes the level of accuracy that the number holds.

res1 - res2 

gives 0.0...00222044604925031

I can't explain that, I'm afraid.

thanks a lot.

You're right I don't know why but I seems like it has to do with precision. If you take 10 for x and y (res will be 1) res1 == res2 is truthy

There are specific rules for which operators take precedence. For the avoidance of doubt, using the parentheses makes it clear which operations are done first rather than trying to plough through a long, unparenthesised set of operators looking at the precendence rules.

As you say, in this example, it makes no difference. But when the precendence is not so clear-cut, it'll make all the difference for readability. The normal rules of BoDMAS don't apply - the precedence is a little different. It is covered in the course.

I looked at the documentation and understood all the precedence levels, but why does res1 == res2 return false?

It must be, yes. The different points at which the precision is applied in the two calculations makes a tiny difference to the outcome.