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

toBigInt should round identically for Double and BigDecimal #3921

Merged
merged 3 commits into from
Mar 12, 2024
Merged

toBigInt should round identically for Double and BigDecimal #3921

merged 3 commits into from
Mar 12, 2024

Conversation

mvanbeirendonck
Copy link
Contributor

This very minor PR fixes a bug in Num.toBigInt(x: BigDecimal, binaryPoint: Int).

The current function gives the impression to round the input BigDecimal:

val result = (x * multiplier).rounded.toBigInt

However:

BigDecimal(1.4).rounded.toBigInt // : BigInt = 1
BigDecimal(1.5).rounded.toBigInt // : BigInt = 1
BigDecimal(1.6).rounded.toBigInt // : BigInt = 1

I'm assuming this is not the intended behaviour.

The underlying issue is with the rounded function which uses the BigDecimal's MathContext. I changed this to use setScale(0) instead.

I added a testcase for rounding positive numbers. I would've added a testcase for negative ones, but as far as I know there is no RoundingMode that replicates the rounding of Doubles there (round half to positive infinity).

Contributor Checklist

  • Did you add Scaladoc to every public function/method?
  • Did you add at least one test demonstrating the PR?
  • Did you delete any extraneous printlns/debugging code?
  • Did you specify the type of improvement?
  • Did you add appropriate documentation in docs/src?
  • Did you request a desired merge strategy?
  • Did you add text to be included in the Release Notes for this change?

Type of Improvement

  • Bugfix

Desired Merge Strategy

  • Squash: The PR will be squashed and merged (choose this if you have no preference).

Release Notes

Reviewer Checklist (only modified by reviewer)

  • Did you add the appropriate labels? (Select the most appropriate one based on the "Type of Improvement")
  • Did you mark the proper milestone (Bug fix: 3.6.x, 5.x, or 6.x depending on impact, API modification or big change: 7.0)?
  • Did you review?
  • Did you check whether all relevant Contributor checkboxes have been checked?
  • Did you do one of the following when ready to merge:
    • Squash: You/ the contributor Enable auto-merge (squash), clean up the commit message, and label with Please Merge.
    • Merge: Ensure that contributor has cleaned up their commit history, then merge with Create a merge commit.

Copy link

linux-foundation-easycla bot commented Mar 8, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: mvanbeirendonck / name: Michiel Van Beirendonck (dac52bd, cf10496)
  • ✅ login: jackkoenig / name: Jack Koenig (62c81fb)

@seldridge seldridge added the Bugfix Fixes a bug, will be included in release notes label Mar 8, 2024
Copy link
Member

@seldridge seldridge left a comment

Choose a reason for hiding this comment

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

Minor comments, but LGTM otherwise. Nice test.

@@ -59,6 +59,20 @@ class LiteralExtractorSpec extends ChiselFlatSpec {
bigIntFromDouble should be(bigInt53)
}

"doubles and big decimals" should "be rounded identically" in {

for (double <- Seq(1.0, 1.1, 1.5, 1.6, 2.5)) {
Copy link
Member

Choose a reason for hiding this comment

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

This has both 1.5 and 2.5 which means that this is checking to make sure it's not using HALF_EVEN. 👍

core/src/main/scala/chisel3/Num.scala Outdated Show resolved Hide resolved
Co-authored-by: Schuyler Eldridge <schuyler.eldridge@gmail.com>
@jackkoenig jackkoenig added this to the 3.6.x milestone Mar 11, 2024
@@ -205,9 +206,9 @@ trait NumObject {
* @param binaryPoint a binaryPoint that you would like to use
* @return
*/
def toBigInt(x: BigDecimal, binaryPoint: Int): BigInt = {
def toBigInt(x: BigDecimal, binaryPoint: Int, roundingMode: RoundingMode = HALF_UP): BigInt = {
Copy link
Contributor

@jackkoenig jackkoenig Mar 11, 2024

Choose a reason for hiding this comment

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

Since this is a bugfix, I'd like to backport it. Stable branches check binary compatibility and obnoxiously in Scala (2 at least, not sure about 3) it is not binary compatible to add an argument with a default parameter--instead you have to just overload the method, i.e.:

def toBigInt(x: BigDecimal): BigInt = toBigInt(x, HALF_UP)
def toBigInt(x: BigDecimal, binaryPoint: Int, roundingMode: RoundingMode): BigInt = ...

I will push this change to your branch, I just wanted to note it before we merge.

Copy link
Contributor

@jackkoenig jackkoenig Mar 11, 2024

Choose a reason for hiding this comment

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

~I just realized the version without the default argument already exists, I'm surprised this isn't causing a problem... ~, nevermind, that's the version taking Double, got it.

@jackkoenig jackkoenig enabled auto-merge (squash) March 12, 2024 15:44
@jackkoenig jackkoenig merged commit b9f5703 into chipsalliance:main Mar 12, 2024
14 checks passed
@mergify mergify bot added the Backported This PR has been backported label Mar 12, 2024
mergify bot pushed a commit that referenced this pull request Mar 12, 2024
---------

Co-authored-by: Schuyler Eldridge <schuyler.eldridge@gmail.com>
Co-authored-by: Jack Koenig <koenig@sifive.com>
(cherry picked from commit b9f5703)

# Conflicts:
#	core/src/main/scala/chisel3/Num.scala
#	src/test/scala/chiselTests/LiteralExtractorSpec.scala
mergify bot pushed a commit that referenced this pull request Mar 12, 2024
---------

Co-authored-by: Schuyler Eldridge <schuyler.eldridge@gmail.com>
Co-authored-by: Jack Koenig <koenig@sifive.com>
(cherry picked from commit b9f5703)
mergify bot pushed a commit that referenced this pull request Mar 12, 2024
---------

Co-authored-by: Schuyler Eldridge <schuyler.eldridge@gmail.com>
Co-authored-by: Jack Koenig <koenig@sifive.com>
(cherry picked from commit b9f5703)
chiselbot pushed a commit that referenced this pull request Mar 12, 2024
…3926)

---------

Co-authored-by: Schuyler Eldridge <schuyler.eldridge@gmail.com>
Co-authored-by: Jack Koenig <koenig@sifive.com>
(cherry picked from commit b9f5703)

Co-authored-by: Michiel Van Beirendonck <michiel.vanbeirendonck@esat.kuleuven.be>
chiselbot pushed a commit that referenced this pull request Mar 12, 2024
…3927)

---------

Co-authored-by: Schuyler Eldridge <schuyler.eldridge@gmail.com>
Co-authored-by: Jack Koenig <koenig@sifive.com>
(cherry picked from commit b9f5703)

Co-authored-by: Michiel Van Beirendonck <michiel.vanbeirendonck@esat.kuleuven.be>
@jackkoenig
Copy link
Contributor

@mvanbeirendonck would you like a release with this fix in the near term? What version of Chisel are you using?

@mvanbeirendonck
Copy link
Contributor Author

@mvanbeirendonck would you like a release with this fix in the near term? What version of Chisel are you using?

Not very urgent! Just wanted to report this one. I am still on Chisel 3.5.6.

@jackkoenig
Copy link
Contributor

@mvanbeirendonck fix released in Chisel v6.3.0 and v5.2.0, hopefully you can get there from Chisel 3.5 😅. This will also be in v3.6.1 when I eventually get to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported This PR has been backported Bugfix Fixes a bug, will be included in release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants