Project

General

Profile

Actions

Bug #13848

closed

BigDecimal.new('200.') raises an exception

Added by ddoherty03 (Daniel Doherty) over 6 years ago. Updated over 5 years ago.

Status:
Closed
Target version:
-
ruby -v:
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
[ruby-core:82512]

Description

BigDecimal.new('200.') raises an exception:

ArgumentError: invalid value for BigDecimal(): "200."

It should accept this as a valid decimal number.

This requires fixing numbers read in from the wild.

Updated by shevegen (Robert A. Heiler) over 6 years ago

I believe you mean for BigDecimal.new('200.') to be equivalent to BigDecimal.new('200.0')?

I could see both ways be ok; there may be use cases where the error is better because of
the user not being explicit enough; or the user does not want to have to type in the
trailing '0'. So I can't say whether this is really a bug or not, it may be specifically
wanted to be that way.

I guess if there are no trade offs though, then I'd agree with the proposal if you meant
that it is the same to mean for a trailing '0' there - but I could also see for purity
to require the '0' to be passed.

What about input such as:

BigDecimal.new('0.')

I mean it will be 0 anyway... it reads strangely to omit the the '0' to my eyes though ...

Latest docu can be found here, if anyone else is interested:

https://ruby-doc.org/stdlib/libdoc/bigdecimal/rdoc/BigDecimal.html

Updated by ddoherty03 (Daniel Doherty) over 6 years ago

Robert, yes, I think it should be the same as BigDecimal.new('200.0'). The problem arises, not when a programmer is typing the number---requiring a programmer to be explicit is OK by me---but when strings are processed that come from a user of the code.

ss = '200.' # Read from a user-prepared file.
BigDecimal.new(ss) => ArgumentError ...

As it is, the programmer has to massage every string to recognize a reasonable, albeit a bit sloppy, string that is clearly meant as a valid number.

Also, it seems crazy to me that

BigDecimal.new('200hello world') => 0.2e3

but BigDecimal('200.') raises an ArgumentError exception. Just sayin...

Regards,

Updated by kernigh (George Koehler) over 6 years ago

Float('200.') also raises ArgumentError. In this way, BigDecimal.new seems consistent with Float. But it's strange that Float('200hello world') raises ArgumentError but BigDecimal.new('200hello world') succeeds.

Updated by mrkn (Kenta Murata) over 6 years ago

  • Status changed from Open to Assigned
  • Assignee set to mrkn (Kenta Murata)
  • Target version set to 2.6
Actions #5

Updated by naruse (Yui NARUSE) about 6 years ago

  • Target version deleted (2.6)

Updated by mrkn (Kenta Murata) over 5 years ago

  • Status changed from Assigned to Closed

Fixed in r66222

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0