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

power and fix methods #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

suleman-uzair
Copy link
Member

This PR adds the support power and fix methods for consistency with BigDecimal-Ruby.

related to => plurimath/plurimath-js#28

@ronaldtse
Copy link

@hmdne are you okay with this? Thanks!

@hmdne
Copy link

hmdne commented Jan 7, 2025

Looks good to me. I will backport it to the opal/opal repo and try to see if we can enable some more rubyspec tests.

@hmdne
Copy link

hmdne commented Jan 7, 2025

In short, all is fine, a couple of additional Rubyspec tests are passing now (https://github.com/ruby/spec/tree/ee83ee683bf15bb2bdd98498069f298abefbdbcc is the repo):

  fails "BigDecimal#** 0 to power of 0 is 1" # NoMethodError: undefined method `**' for 0
  fails "BigDecimal#** 0 to powers < 0 is Infinity" # NoMethodError: undefined method `**' for 0
  fails "BigDecimal#** other powers of 0 are 0" # NoMethodError: undefined method `**' for 0
  fails "BigDecimal#** powers of 1 equal 1" # NoMethodError: undefined method `**' for 1
  fails "BigDecimal#** returns 0.0 if self is infinite and argument is negative" # NoMethodError: undefined method `**' for Infinity
  fails "BigDecimal#** returns NaN if self is NaN" # NoMethodError: undefined method `**' for NaN
  fails "BigDecimal#** returns infinite if self is infinite and argument is positive" # NoMethodError: undefined method `**' for Infinity
  fails "BigDecimal#fix correctly handles special values" # NoMethodError: undefined method `fix' for Infinity
  fails "BigDecimal#fix does not allow any arguments" # Expected ArgumentError but got: NoMethodError (undefined method `fix' for 1.23456789)
  fails "BigDecimal#fix returns 0 if the absolute value is < 1" # NoMethodError: undefined method `fix' for 0.99999
  fails "BigDecimal#fix returns a BigDecimal" # NoMethodError: undefined method `fix' for Infinity
  fails "BigDecimal#fix returns the integer part of the absolute value" # NoMethodError: undefined method `fix' for 2e+1000
  fails "BigDecimal#power 0 to power of 0 is 1" # NoMethodError: undefined method `power' for 0
  fails "BigDecimal#power 0 to powers < 0 is Infinity" # NoMethodError: undefined method `power' for 0
  fails "BigDecimal#power other powers of 0 are 0" # NoMethodError: undefined method `power' for 0
  fails "BigDecimal#power powers of 1 equal 1" # NoMethodError: undefined method `power' for 1
  fails "BigDecimal#power returns 0.0 if self is infinite and argument is negative" # NoMethodError: undefined method `power' for Infinity
  fails "BigDecimal#power returns NaN if self is NaN" # NoMethodError: undefined method `power' for NaN
  fails "BigDecimal#power returns infinite if self is infinite and argument is positive" # NoMethodError: undefined method `power' for Infinity

But also there are new failures, which probably were disabled due to missing functions, some of which you may want to correct for better compatibility.

fails "BigDecimal#truncate returns NaN if self is NaN" # ArgumentError: [BigDecimal#fix] wrong number of arguments (given 1, expected 0)
fails "BigDecimal#truncate returns value of type Integer." # Expected false == true to be truthy but was false
fails "BigDecimal#truncate sets n digits left of the decimal point to 0, if given n < 0" # ArgumentError: [BigDecimal#fix] wrong number of arguments (given 1, expected 0)

@suleman-uzair
Copy link
Member Author

@hmdne, I have fixed the argument error, and now there is an error related to BigNumber’s support for negative integers as the truncate method’s argument.
As far as I could explore the BigNumber v2.1.4, the minimum integer allowed for round (called in truncate) is 0, while BigDecimal supports negative integers.

for example In Ruby:

BigDecimal("1212.11").truncate(-1).to_s("F") # "1210.0"

But in BigNumber this will raise the following error (as mentioned here):

decimal places out of range

can we skip the negative integers support for now or do we need this implementation?

cc: @ronaldtse

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.

3 participants