Commit graph

118 commits

Author SHA1 Message Date
Jason Biegel
a36b5d85f3
Merge pull request #141 from kardianos/kardianos-decomposer
decimal: add decomposer interface
2019-09-05 10:42:23 -04:00
Daniel Theophanes
75bb2cb98e decimal: add decomposer interface
For golang/go#30870
2019-09-04 15:36:13 -07:00
Klaus Post
cd690d0c9e Make NewFromFloat respect the precision of the input (#100)
* Make NewFromFloat respect the precision of the input

Restores the previous behaviour where input converted from float is truncated at the precision of the float.

The precision is depending on the actual value. Simply making it 15 digits would make it faster, but would in some cases lose some precision. So the code from the stdlib that does this calculation (very well) has been included.

Lots of good articles here: https://www.exploringbinary.com/decimal-precision-of-binary-floating-point-numbers/

Performance is around the same as the previous string roundtrip since it basically does the same, but allocations are a bit less.

`BenchmarkNewFromStringFloat` is the old method, `BenchmarkNewFromFloat` is the new.

```
BenchmarkNewFromFloatWithExponent-8     10000000               260 ns/op             174 B/op          4 allocs/op
BenchmarkNewFromFloat-8                  2000000               744 ns/op              90 B/op          2 allocs/op
BenchmarkNewFromStringFloat-8            2000000               822 ns/op             258 B/op          6 allocs/op
```

* Update Sin/Tan/Cos tests.
2018-07-09 16:31:17 -04:00
Ian Norden
7e43aed1c9 Added cos, sin, tan, and atan methods and tests (#96)
* Added Atan method

* added sin and cos methods

* added tests for atan, sin, and cos

* tan method and test

* potentinal fix for './decimal_test.go:261:6: d declared but not used' error thrown by the tip version Travis CI tests + corrected comment in decimal.go
2018-07-09 10:22:25 -04:00
José David Arévalo
19e3cb6c29 add functions IsPositive, IsNegative and IsZero (#88) 2018-06-07 10:48:47 -04:00
Steven Roose
2df3e6ddaf Add support for shifting in base 10 (#86)
This allows for very fast multiplying and dividing by powers of 10
without the possibility of losing precision.
2018-03-19 13:08:23 -04:00
Randy Pensinger
69b3a8ad1f RequireFromString (#73)
fmt
2018-03-01 16:32:36 -05:00
Igor Mikushkin
ca6009d707 Testing Float64 (#82)
* Additional (and some breaking) tests for NewFromFloatWithExponent

* Addressing tests for NewFromFloatWithExponent

* Naming cosmetic correction

* removing unused code

* Improving FromFloatWithExponent

* Tests for exact float representation added

* Exact float representation in FromFloat

* Adding breaking test for NewFromFloat

* Fast path in FromFloat is unreliable, fixing it

* Addressing special meaning of zero exponent in float64

* NewFromFloatWithExponent: subnormals support

* NewFromFloatWithExponent: just a few additional test cases

* NewFromFloat: documentation update

* NewFromFloatWithExponent: optimization and some documentation

* NewFromFloatWithExponent: optimizations

* NewFromFloatWithExponent: optimizations

* Subnormal test added

* Float64: adding tests
2018-03-01 16:31:53 -05:00
Igor Mikushkin
78e9b82f68 Exact representation in NewFromFloat (#78)
* Additional (and some breaking) tests for NewFromFloatWithExponent

* Addressing tests for NewFromFloatWithExponent

* Naming cosmetic correction

* removing unused code

* Improving FromFloatWithExponent

* Tests for exact float representation added

* Exact float representation in FromFloat

* Adding breaking test for NewFromFloat

* Fast path in FromFloat is unreliable, fixing it

* Addressing special meaning of zero exponent in float64

* NewFromFloatWithExponent: subnormals support

* NewFromFloatWithExponent: just a few additional test cases

* NewFromFloat: documentation update

* NewFromFloatWithExponent: optimization and some documentation

* NewFromFloatWithExponent: optimizations

* NewFromFloatWithExponent: optimizations
2018-03-01 16:29:43 -05:00
Igor Mikushkin
bf9a39e28b Addressing rounding and overflow issues in NewFromFloatWithExponent (#77)
* Additional (and some breaking) tests for NewFromFloatWithExponent

* Addressing tests for NewFromFloatWithExponent

* Naming cosmetic correction

* removing unused code

* Improving FromFloatWithExponent

* Addressing special meaning of zero exponent in float64

* NewFromFloatWithExponent: subnormals support

* NewFromFloatWithExponent: just a few additional test cases

* NewFromFloatWithExponent: optimization and some documentation

* NewFromFloatWithExponent: optimizations

* NewFromFloatWithExponent: optimizations
2018-03-01 16:29:13 -05:00
ivahaev
e3482495ff Added ensureInitialized() to the Neg() method (#80)
* Added ensureInitialized to the Neg() method,
because it can panic when d.value is nil

* TestDecimal_NegFromEmpty added
2018-01-26 16:25:43 -05:00
Chad Gilbert
a76a8bf60b Fix broken RoundBank func in #75 (#76) 2018-01-18 17:52:17 -05:00
Zheng Dayu
8dffa2b7c7 format usage code in README (#70) 2018-01-02 14:16:03 -05:00
tbrs
9ca7f51822 Fix Floor() and Ceil() for integer values (#64)
Implementation of aforementioned methods applied an integer multiplier
to int part, and rejected the exponent. This did not work well for
positive exponent values - multiplier was supposed to be a non-integer
value less than 1, but was rounded up to it. This caused different
results for equal Decimal values like decimal.New(19, 1) and
decimal.New(1900, -1).

Now functions return the receiver if it represents an integer value.
This also reduces execution time for previously broken cases.
2017-11-08 17:52:54 -05:00
Cyrill Schumacher
faaed5fca7 Implement Swedish/Cash rounding fixes #63 (#66)
Performance and allocations not that good but can be optimized later.

RoundSwedish aka Cash/Penny/öre rounding rounds decimal to a specific
interval. The amount payable for a cash transaction is rounded to the nearest
multiple of the minimum currency unit available. The following intervals are
available: 5, 10, 15, 25, 50 and 100; any other number throws a panic.
    5:   5 cent rounding 3.43 => 3.45
   10:  10 cent rounding 3.45 => 3.50 (5 gets rounded up)
   15:  10 cent rounding 3.45 => 3.40 (5 gets rounded down)
   25:  25 cent rounding 3.41 => 3.50
   50:  50 cent rounding 3.75 => 4.00
  100: 100 cent rounding 3.50 => 4.00
For more details: https://en.wikipedia.org/wiki/Cash_rounding

BenchmarkDecimal_RoundSwedish/five-4         	 1000000	      1918 ns/op	    1164 B/op	      30 allocs/op
BenchmarkDecimal_RoundSwedish/fifteen-4      	  300000	      4331 ns/op	    2940 B/op	      74 allocs/op
2017-10-20 10:00:25 -04:00
MOZGIII
ad668bb369 Added JSON support for NullDecimal (#62)
* Corrected NullDecimal comment

Saying that NullDecimal is immutable is just incorrect, and we don't need
to duplicate the description of properties of a Decimal type here.

* Added UnmarshalJSON and MarshalJSON support for NullDecimal

* Added tests for JSON operations on NullDecimal

* Fixed mistakes in tests
2017-10-10 12:21:51 -04:00
Rigel
aed1bfe463 Added Banker Rounding (#61)
* made GTE/GT/LT/LTE go-lint compliant

* added banker rounding and tests (RoundBank, StringFixedBank)
2017-09-11 08:58:34 -04:00
Dan Mayor
730de27aa5 Suggesting Sum and Avg methods (provided unit tests), recommending removing val2 from for range loops in TestBinary & TestGobEncode (#60) 2017-09-05 09:57:20 -04:00
klokare
b9ab2bce74 Add null support to UnmarshalJSON (#58)
Unmarshal null into zero value
2017-08-15 22:25:21 -04:00
Matthew Lane
3c692774ac Added NewFromBigInt constructor & unit test (#56) 2017-07-28 15:12:53 -04:00
Rigel
16a9418214 added GT, GTE, LT, LTE (#54)
* added GT, GTE, LT, LTE

* GTE -> GreaterThanOrEqual
2017-05-17 18:01:17 -04:00
mattdee123
d5fda14026 Defensive Copy on Coefficient (#52) 2017-05-17 10:58:21 -04:00
Bastien Gysler
3868940cdd Fix GobDecode (#51)
This commit fixes GobDecode, which were mutating a copy of the receiver, rather than the intended pointed struct.
2017-05-09 08:51:20 -04:00
Jason Biegel
3526cd0bdb Added negative pow test 2017-02-23 11:50:30 -05:00
Evan Shaw
4b9150b187 Add Neg method (#48)
Neg provides an easy way to negate a Decimal.
2017-02-21 13:39:36 -05:00
Victor Quinn
77e6b496f6 Fix same issue as #39 but without changing the Value() interface 2017-02-21 13:34:52 -05:00
Victor Quinn
ed31524af4 Add GobEncode and GobDecode, fixes the issue raised in #23 but with Marshal/UnmarshalBinary 2017-02-21 13:23:18 -05:00
klokare
15474de08e Binary Marshalling Support (#49)
Adds MarshalBinary and UnmarshalBinary methods
2017-02-19 22:31:54 -05:00
Victor Quinn
4570ccd0d5 Merge pull request #38 from edsrzf/sign
Add Sign method
2017-02-15 21:22:53 -05:00
Victor Quinn
4ec03b4267 gofmt -s decimal_test.go 2017-02-15 19:08:34 -05:00
Victor Quinn
d48ced5acf Fix some grammar, go vet issues 2017-02-15 19:06:17 -05:00
Victor Quinn
abf4647f0d Fix misspellings 2017-02-15 19:03:27 -05:00
Victor Quinn
9f5251621e Back out travis lint as it breaks old versions of go 2017-02-15 18:59:22 -05:00
Victor Quinn
2aa9963045 Pass lint, add travis stuff to run golint 2017-02-15 18:55:38 -05:00
Victor Quinn
868a37b979 Increase test coverage a bit 2017-02-15 18:47:10 -05:00
Victor Quinn
de8af6d48f Merge pull request #42 from chrisguiney/expose-coefficient
Expose a Coefficient() function
2017-02-15 17:39:50 -05:00
Victor Quinn
c0815674f8 Merge pull request #32 from craigjackson/null_decimal
Added NullDecimal. Changed Decimal to implement driver.Valuer.

NOTE: This is potentially a breaking change as it reverts #16
2017-02-15 17:37:35 -05:00
Victor Quinn
f37fa05388 Merge pull request #46 from njason/master
Remove insignificant digits during string parsing
2017-02-15 12:26:51 -05:00
Jason Biegel
4e5e2ed948 Remove insignificant digits during string parsing 2017-02-07 12:33:26 -05:00
Evan Shaw
1aec7720a8 Add Sign method
d.Sign() is a more convenient way of saying d.Cmp(Zero).
2017-02-07 10:46:18 +13:00
Victor Quinn
5471f2c322 Merge pull request #44 from bgaifullin/master
Added method Equal instead of method Equals
2017-02-06 10:49:37 -05:00
Bulat Gaifullin
0460d2dd77 Added method Equal instead of method Equals 2017-02-06 18:46:03 +03:00
Victor Quinn
b8c85eb1cf Merge pull request #29 from martinlindhe/pow
add Pow() (fix #26)
2017-02-03 18:13:37 -05:00
Victor Quinn
ea75adedfe Merge pull request #12 from raverkamp/master
replaced divsion code
2017-01-30 10:08:29 -05:00
Victor Quinn
2ad742c865
Fix some typos, add godoc badge 2017-01-28 09:42:33 -05:00
Chris Guiney
c1c27c221b Expose a Coefficient() function 2017-01-16 14:54:52 -08:00
Craig Jackson
f03103a27d Have NullDecimal.Value() use Decimal.Value(). 2016-11-10 16:07:19 -07:00
Craig Jackson
02487a179e Added NullDecimal. Changed Decimal to implement driver.Valuer.
From comments in PR 16.
2016-11-02 10:15:14 -06:00
Martin Lindhe
8a97b04143 add Pow() 2016-09-19 23:54:54 +02:00
Vadim Graboys
d6f52241f3 allow optional marshalling JSON without quotes 2016-09-18 13:52:01 -07:00