Skip to content

feat: add d2:log and d2:exponent to program rule grammar#101

Open
superskip wants to merge 6 commits into
mainfrom
d2-pow
Open

feat: add d2:log and d2:exponent to program rule grammar#101
superskip wants to merge 6 commits into
mainfrom
d2-pow

Conversation

@superskip

@superskip superskip commented May 29, 2026

Copy link
Copy Markdown
Collaborator

Things to look at:

  • There already was a log function in Calculator.kt, and I just copy-pasted the implementation. A more clever solution might exist.
  • Kotlin uses the name pow for "raising a number to a power", and I think C++ uses the same name. Personally I associate this operation more with the name exp. The advocates for pow probably argue that exp is another function (e^x). Regardless, I tend to look up exp rather than pow if I want to "exponentiate" something, and chances are this is true for other people as well.

Update:

  • Combined the implementation of log and d2_log.
  • Changed name from pow to exponent (as suggested in the roadmap).

@superskip superskip requested review from enricocolasante and jbee and removed request for jbee May 29, 2026 13:07
@sonarqubecloud

sonarqubecloud Bot commented Jun 1, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
1 New issue
1 New Vulnerabilities (required ≤ 0)
C Security Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@superskip superskip changed the title feat: add d2:log and d2:pow to program rule grammar feat: add d2:log and d2:exponent to program rule grammar Jun 1, 2026
/**
* Tests the `pow` function
*
* @author Jan Bernitt / Tony Valle

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I was not the author of this file ;)

@superskip superskip Jun 8, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Should I swap the order of the names or remove your name entirely? (I kept it because the file is basically a literal translation of the log test)

fun testExponent() {
assertEquals(1.0, evaluate("d2:exponent(49, 0)"))
assertEquals(7.0, evaluate("d2:exponent(49, 0.5)"))
assertEquals(49.0, evaluate("d2:exponent(49, 1)"))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

How about at least one case where we use the function with a exponent > 1?

NamedFunction.isNull -> functions.isNull(evalToMixed(fn.child(0)))
NamedFunction.least -> functions.least(evalToNumbers(fn.children()))
NamedFunction.log -> if (fn.size() == 1) functions.log(evalToNumber(fn.child(0)))
NamedFunction.log, NamedFunction.d2_log -> if (fn.size() == 1) functions.log(evalToNumber(fn.child(0)))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is there a reason to add d2:log when log already exists?

@superskip superskip Jun 8, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The reason is that all other program rule functions use the d2:-prefix.

- Edit author
- Correction of the test title
- Adds a test case for exponent > 1 and exponent < 0
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