Interest_is_interesting_test.go has a weird testing value

Using Windows Calculator and multiplying 1000 with (1 + 1.621%) 2x yields 1032.6827641 which is off by 0.000001046664 from the original testing value 1032.682765146664.

The issue was originally reported on Discord Discord

This is because the user’s code is using the wrong datatype. The datatype used matters. float32 math and float64 math don’t give the same results.

@IsaacG

I’m having the same issue from Exercism’s web editor. The code all uses and returns float64s, but the test OP was referring to also gives me 1032.6827640999999858 for the second iteration when the test expects 1032.682765146664.

So it fails as my code returns 3 years to get the desired balance when the test expects 2 years.

Link to exercise

If I had to guess, I’d say there’s a type conversion issue somewhere in your solution. My solution passes the tests. Assuming you solve it the same way I did, it should pass the tests. Without seeing what you’re doing differently, I can’t really help much. Would you like to share your code?

package interest

const (
    negativeInterestRate = 3.213
    smallInterestRate    = 0.5
    mediumInterestRate   = 1.621
    largeInterestRate    = 2.475
    
    smallInterestThreshold  = 1000
    mediumInterestThreshold = 5000
)

func InterestRate(balance float64) float64 {
    if balance < 0 {
        return negativeInterestRate
    }
    
    if balance < smallInterestThreshold {
        return smallInterestRate
    }
    
    if balance < mediumInterestThreshold {
        return mediumInterestRate
    }
    
    return largeInterestRate
}

// Interest calculates the interest for the provided balance.
func Interest(balance float64) float64 {
    return balance * (InterestRate(balance) / 100.0)
}

// AnnualBalanceUpdate calculates the annual balance update, taking into account the interest rate.
func AnnualBalanceUpdate(balance float64) float64 {
    return balance + Interest(balance)
}

// YearsBeforeDesiredBalance calculates the minimum number of years required to reach the desired balance.
func YearsBeforeDesiredBalance(balance, targetBalance float64) int {
    numYears := 0

    for targetBalance > balance {
        balance = AnnualBalanceUpdate(balance)
        numYears++
    }

    return numYears
}

Take another look at task 1.

Note that the value returned is a float32.

Missed that, thanks!