Skip to content

Conversation

@DurvalMenezes
Copy link
Contributor

For your appreciation. Will update with autotest code later.

if x < 1 || x > 45 {
// less than one is outside the domain of the mathematical function,
// and more than 45 overflows uint64 and results in infinite recursion
panic("fib() called with too small or large value!")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Panic will crash the program with a huge and ugly stack trace. It's never the best solution. If we want to quit the program with a fatal condition, log.Fatal is the way to go. Here, I think this should return an error that is then "bubbled up" to the caller and displayed to the user.


ophandler{"fac", "Calculate factorial of x", 1, func(a []float64) ([]float64, int, error) {
x := uint64(a[0])
if float64(x) != a[0] {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing this maneuver with uint64 and float to make sure the number is an integer, maybe use if math.Floor(a) != a { ... } like elsewhere in the code. This avoids the confusing type casting.

return []float64{float64(fact)}, 1, nil
}},

ophandler{"fib", "Calculate fibonacci of x", 1, func(a []float64) ([]float64, int, error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not design fib() in a way that it can be called directly from the ophandler?


ophandler{"fib", "Calculate fibonacci of x", 1, func(a []float64) ([]float64, int, error) {
x := a[0]
if x-float64(uint64(x)) != 0 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use the same math.Floor approach here?

if x-float64(uint64(x)) != 0 {
return nil, 1, errors.New("fibonacci requires an integer number")
}
if x < 1. {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need a dot at the end in Go. if x < 1 works fine :)

if x < 1. {
return nil, 1, errors.New("fibonacci requires a positive number")
}
if x > 45. {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here (dot)


ophandler{"fib", "Calculate fibonacci of x", 1, func(a []float64) ([]float64, int, error) {
x := a[0]
if x-float64(uint64(x)) != 0 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With a bunch of ifs, we can use a switch. It's cleaner. Remember that in Go, a case is not restricted to one variable. You can do:

switch {
  case any_condition:
    (...)
  case any_other_condition:
    (...)
}

and so on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants