Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Suggestion] An exercise about numeric limits in Go #1109

Closed
exklamationmark opened this issue May 19, 2018 · 3 comments
Closed

[Suggestion] An exercise about numeric limits in Go #1109

exklamationmark opened this issue May 19, 2018 · 3 comments

Comments

@exklamationmark
Copy link
Contributor

Do we have any exercise that deals with the numeric limits of integer/floating point types in Go (e.g: int, int32, int64, float32, float64, etc) ?

I have been doing a few math-based exercises, e.g: difference-of-squares, space-age, etc and haven't seen things like overflow, underflow get touched.

IMO, this might make for an interesting intermediate/advanced exercise, especially for those who will use Go professionally.

wdyt?

@exklamationmark exklamationmark changed the title [Suggestion] An exercises about numeric limits in Go [Suggestion] An exercise about numeric limits in Go May 19, 2018
@leenipper
Copy link
Contributor

As you probably know, all the exercises get their genesis from the exercises in problem-specifications.

Do you have any specific ideas for an exercise related to numeric limits?

We can begin the discussion here a bit, and then you could submit a suggested exercise to the problem-specifications. Any new exercise in problem-specifications should be very language agnostic, naturally.

@exklamationmark
Copy link
Contributor Author

exklamationmark commented Jun 5, 2018

Hi @leenipper,

IMO, knowing what computer can realistically represents is usually a good thing (especially when working with money & high-precision calculations). There are 2 parts: general way numbers are handled by computer and language-specific quirks.

For the general stuff, how about an exercises on evaluating a user-defined expressions. For example, if we have:

func EvalInt(expr string, map[string]int variables) int { ... }
// EvalInt("x + y", map[string]int{"x":1, "y":2}) -> 3

func EvalFloat64(expr string, map[string]float64 variables) { ... }
// EvalFloat64("x/2", map[string]float64{"x":4.0}) -> 2.0

we can introduce a few concepts:

userExpr := "x + y"
variables := map[string]int{
    "x": math.MaxInt32,
    "y": math.MaxInt32,
}
result := EvalInt(userExpr, variables) // -> -2, due to integer overflow
userExpr := "x/2"
variables := map[string]float64{
    "x": math.SmallestNonzeroFloat64,
}
result := EvalFloat64(userExpr, variables) // -> 0, due to floating point underflow

For language-specific part, I think an interesting exercise is to implement the Round() function (Go 1.10).

Go's approach to this is different from some other languages (say Javascript), so it might be worth knowing.

This article on rounding is interesting, too

@junedev
Copy link
Member

junedev commented Oct 18, 2021

Closing this as it wasn't picked up in 3 years. In the v3 system, numeric limits are more a topic for the about.md file of some concept.

@junedev junedev closed this as completed Oct 18, 2021
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

No branches or pull requests

3 participants