Comments (5)
Also another (potentially serious) issue. If you pass "1.", to_f & to_i accept it, but to_d throws an exception.
2.4.1 :001 > require 'bigdecimal'
2.4.1 :002 > require 'bigdecimal/util'
2.4.1 :003 > "1.".to_i
=> 1
2.4.1 :004 > "1.".to_f
=> 1.0
2.4.1 :005 > "1.".to_d
ArgumentError: invalid value for BigDecimal(): "1."
from bigdecimal.
I'm seeing something even more worrying with bigdecimal-1.3.2
2.4.1 :001 > require 'bigdecimal'
=> true
2.4.1 :002 > require 'bigdecimal/util'
=> true
2.4.1 :006 > "1.".to_i
=> 1
2.4.1 :007 > "1.".to_f
=> 1.0
2.4.1 :008 > "1.".to_d
=> 0.0
from bigdecimal.
I'm also seeing this issue. Looks like it's somewhat related to this fix: #55
In ruby 2.3.3:
>> "30.".to_d
=> #<BigDecimal:55adc4a76918,'0.3E2',9(18)>
In ruby 2.4.1 (and updating bigdecimal to 1.3.2):
>> "30.".to_d
=> 0.0
from bigdecimal.
also
> "1.23other".to_d
=> 0.123e1
> "1.23degree".to_d
=> 0.0
from bigdecimal.
I guess the problem it that:
to_d tries to operate with "e"(also does the same with "d") on the string to multiplies or divide by 10 times the value that succeds the letter, but when encounters another letter, it somehow sets the value to 0.0 while to_f ignores the word.
that's why stuff like that happens:
> "1.23other".to_d
=> 0.123e1
> "1.23degree".to_d
=> 0.0
> "1.23kelvin".to_d
=> 0.123e1
> "1.23degree".to_f
=> 1.23
I'm having some struggle with a validation, because when it tries to validates an string with my regex it refuses stuff like "41avocado" but shows to the user the value "41.00" with error, cause after the typecast, the word is just ignored, but if the letter after the number was "e" or "d" the value is set to "0.00", wich is more user-friendly to be understood...
from bigdecimal.
Related Issues (20)
- "ERROR(VpDivd): space for remainder too small" for certain division HOT 3
- Exponential growth in precision causing memory usage to spike as of 3.1.2 HOT 5
- ERROR(VpDivd): space for remainder too small
- BigDecimal#round raises FloatDomainError regardless of exception mode HOT 2
- Comparison failure: 9.8.abs <= BigDecimal('9.8') => false HOT 4
- Inconsistent max precision affecting dump&load result HOT 4
- Inconsistency between Bigdecimal 3.0.x and 3.1.x HOT 4
- BigDecimal#n_significant_digits and #precision do not return the correct value HOT 1
- I would expect BigDecimal#divmod to return type [Int, BigDecimal] (like Integer#divmod, Float#divmod)
- mode(BigDecimal::EXCEPTION_ALL,true) did not throw an exception when bigdecimal#exponent overflowed HOT 1
- Integrate JRuby implementation into the gem
- Loss of precision on very large numbers in `to_d` HOT 1
- Incorrect calculation result after upgrading from BigDecimal 3.1.1 to 3.1.2 HOT 1
- Difference in behavior with and without BigDecimal#dev 'digit' argument
- nil.to_d inconsistent (bigdecimal/util)
- Installation Error of BigDecimal 3.1.5 in JRuby HOT 3
- ruby 3.0.6 compilation error on MacOS Sonoma 14 related to maybe_unused HOT 1
- Ruby 3.4 and BigDecimal. HOT 7
- Memory leak with BigDecimal::EXCEPTION_OVERFLOW HOT 2
- BigDecimal#whole? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bigdecimal.