Skip to content

lang: funcs: Add epsilon checks to operator_polyfunc#578

Open
dbakong wants to merge 1 commit into
purpleidea:masterfrom
dbakong:feat/add-epsilon-check
Open

lang: funcs: Add epsilon checks to operator_polyfunc#578
dbakong wants to merge 1 commit into
purpleidea:masterfrom
dbakong:feat/add-epsilon-check

Conversation

@dbakong

@dbakong dbakong commented Dec 31, 2019

Copy link
Copy Markdown
Contributor

Tips:

  • please read the style guide before submitting your patch:
    docs/style-guide.md

  • commit message titles must be in the form:

topic: Capitalized message with no trailing period

or:

topic, topic2: Capitalized message with no trailing period

  • golang code must be formatted according to the standard, please run:
make gofmt		# formats the entire project correctly

or format a single golang file correctly:

gofmt -w yourcode.go
  • please rebase your patch against current git master:
git checkout master
git pull origin master
git checkout your-feature
git rebase master
git push your-remote your-feature
hub pull-request	# or submit with the github web ui
  • after a patch review, please ping @purpleidea so we know to re-review:
# make changes based on reviews...
git add -p		# add new changes
git commit --amend	# combine with existing commit
git push your-remote your-feature -f
# now ping @purpleidea in the github PR since it doesn't notify us automatically

Thanks for contributing to mgmt and welcome to the team!

@purpleidea

Copy link
Copy Markdown
Owner

@dbakong I think you're on the right track, the epsilon stuff should be done at the top and only once. As either a var or a const or an init() or a combination. See what works =D

@dbakong dbakong force-pushed the feat/add-epsilon-check branch from 4a46cde to 65e8b75 Compare December 31, 2019 01:09
@dbakong

dbakong commented Dec 31, 2019

Copy link
Copy Markdown
Contributor Author

@purpleidea thanks, I moved it to the top.

@dbakong dbakong force-pushed the feat/add-epsilon-check branch 3 times, most recently from 0c043c6 to a8cca31 Compare December 31, 2019 01:40
Comment thread lang/funcs/operator_polyfunc.go Outdated
)

func init() {
epsilon := func() float64 {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I bet you can do even better! this computation still repeats each time.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

updated!

@dbakong dbakong force-pushed the feat/add-epsilon-check branch 2 times, most recently from 0d05448 to 1ef8e91 Compare December 31, 2019 14:40
Comment thread lang/funcs/operator_polyfunc.go Outdated
operatorArgName = "op" // something short and arbitrary
)

var epsilon = func() float64 {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

  1. How can you simplify this?

  2. It still runs for each use of the operator.

Can we do better?

@dbakong dbakong force-pushed the feat/add-epsilon-check branch from 1ef8e91 to 0398644 Compare January 1, 2020 18:37

@purpleidea purpleidea left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Since you have this pattern:

math.Abs(input[0].Float()-input[1].Float()) > epsilon

Repeated very often, maybe it makes sense to have it be a helper function? Write this as a lambda, right under the epsilon definition. Secondly, check the > above. Is it that or do we want >= ?

Being precise about > vs >= is exactly the point of this problem. Review all the instances in your patch and clean up the other issues mentioned and resubmit!

I hope this is fun!
Thanks!

Comment thread lang/funcs/operator_polyfunc.go Outdated
V: func(input []types.Value) (types.Value, error) {
// TODO: should we do an epsilon check?
// epsilon check
if math.Abs(input[0].Float()-input[1].Float()) <= epsilon {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Should this be < instead of <= ?

Comment thread lang/funcs/operator_polyfunc.go Outdated
V: func(input []types.Value) (types.Value, error) {
// TODO: should we do an epsilon check?
// epsilon check
if math.Abs(input[0].Float()-input[1].Float()) <= epsilon {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think the more elegant way to write this is to start off with something like:

result := false

And then in you condition you do: result = true

And at the end you put in the bool into the &BoolValue as result. (Similarly for other cases.)

Comment thread lang/funcs/operator_polyfunc.go Outdated
V: func(input []types.Value) (types.Value, error) {
// TODO: should we do an epsilon check?
// epsilon check
if math.Abs(input[0].Float()-input[1].Float()) > epsilon {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

> or >= ?

)

func init() {
// epsilon definition

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This is an unhelpful comment. Explain why this is the case, since the code is not intuitive.

@dbakong dbakong force-pushed the feat/add-epsilon-check branch from 0398644 to bb8d5da Compare March 4, 2020 01:49
@purpleidea

Copy link
Copy Markdown
Owner

@dbakong The epsilon helper doesn't make sense... Where is input coming from?

Put the patch back to the way it was before and get all the < > <= >= stuff perfect. LMK here when ready.

@dbakong dbakong force-pushed the feat/add-epsilon-check branch 2 times, most recently from 297f4b4 to 8e3372a Compare March 12, 2020 03:30
@dbakong

dbakong commented Mar 12, 2020

Copy link
Copy Markdown
Contributor Author

@purpleidea I think I got the last missing operator update. Please review.

@dbakong dbakong force-pushed the feat/add-epsilon-check branch from 8e3372a to 02f7232 Compare March 12, 2020 04:24
@dbakong dbakong force-pushed the feat/add-epsilon-check branch 2 times, most recently from 8f68996 to 372b5ef Compare March 22, 2020 22:22
@dbakong dbakong force-pushed the feat/add-epsilon-check branch from 372b5ef to d2586d2 Compare April 3, 2020 23:42
@dbakong dbakong force-pushed the feat/add-epsilon-check branch from d2586d2 to 6da48d2 Compare April 6, 2020 00:08
@purpleidea purpleidea force-pushed the master branch 4 times, most recently from 2319f4f to 04f5ba6 Compare September 12, 2022 01:44
@purpleidea purpleidea force-pushed the master branch 2 times, most recently from 0995279 to a665861 Compare December 4, 2023 02:25
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