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

[WIP] High level api #2

Merged
merged 15 commits into from
Aug 20, 2019
Merged
94 changes: 94 additions & 0 deletions decimal/decimal.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,97 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms.

import decimal_lowlevel

type
DecimalType = ref[ptr mpd_t]

const
DEFAULT_PREC = MPD_RDIGITS * 2
DEFAULT_EMAX = when (sizeof(int) == 8): 999999999999999999 else: 425000000
DEFAULT_EMIN = when (sizeof(int) == 8): -999999999999999999 else: -425000000

var CTX: mpd_context_t
var CTX_ADDR = addr CTX
mpd_defaultcontext(CTX_ADDR)
HugoGranstrom marked this conversation as resolved.
Show resolved Hide resolved

proc setPrec*(prec: int) =
if 0 < prec:
let success = mpd_qsetprec(CTX_ADDR, prec)
if success == 0:
echo "Bad prec"

proc `$`*(s: DecimalType): cstring = mpd_to_sci(s, 0)

proc newDecimal*(): DecimalType = mpd_new(CTX_ADDR)

proc newDecimal*(s: string): DecimalType =
result = mpd_new(CTX_ADDR)
mpd_set_string(result, s, CTX_ADDR)

proc newDecimal*(s: int64): DecimalType =
result = mpd_new(CTX_ADDR)
mpd_set_i64(result, s, CTX_ADDR)

proc newDecimal*(s: int32): DecimalType =
result = mpd_new(CTX_ADDR)
mpd_set_i32(result, s, CTX_ADDR)

proc clone*(b: DecimalType): DecimalType =
result = newDecimal()
mpd_copy(result, b, CTX_ADDR)

proc `+`*(a, b: DecimalType): DecimalType =
result = newDecimal()
mpd_add(result, a, b, CTX_ADDR)

proc `+=`*(a, b: DecimalType) =
mpd_add(a, a, b, CTX_ADDR)

proc `-`*(a, b: DecimalType): DecimalType =
result = newDecimal()
mpd_sub(result, a, b, CTX_ADDR)

proc `-=`*(a, b: DecimalType) =
mpd_sub(a, a, b, CTX_ADDR)

proc `*`*(a, b: DecimalType): DecimalType =
result = newDecimal()
mpd_mul(result, a, b, CTX_ADDR)

proc `*=`*(a, b: DecimalType) =
mpd_mul(a, a, b, CTX_ADDR)

proc `/`*(a, b: DecimalType): DecimalType =
result = newDecimal()
mpd_div(result, a, b, CTX_ADDR)

proc `/=`*(a, b: DecimalType) =
mpd_div(a, a, b, CTX_ADDR)

proc `^`*(a, b: DecimalType): DecimalType =
result = newDecimal()
mpd_pow(result, a, b, CTX_ADDR)

proc divint*(a, b: DecimalType): DecimalType =
result = newDecimal()
mpd_divint(result, a, b, CTX_ADDR)

proc `//`*(a, b: DecimalType): DecimalType =
result = newDecimal()
mpd_divint(result, a, b, CTX_ADDR)
HugoGranstrom marked this conversation as resolved.
Show resolved Hide resolved

proc rem*(a, b: DecimalType): DecimalType =
result = newDecimal()
mpd_rem(result, a, b, CTX_ADDR)

proc divmod*(a, b: DecimalType): (DecimalType, DecimalType) =
let q = newDecimal()
let r = newDecimal()
mpd_divmod(q, r, a, b, CTX_ADDR)
result = (q, r)


proc exp*(a: DecimalType): DecimalType =
result = newDecimal()
mpd_exp(result, a, CTX_ADDR)

37 changes: 34 additions & 3 deletions tests/all_tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,37 @@
import unittest,
../decimal/decimal

suite "Mock compile test":
test "Nim-decimal and its wrapped library compiles":
discard
suite "Basic Arithmetic":
test "Set Decimal from string":
let s = "1.23456"
var d = newDecimal(s)
check $d == s
test "Set Decimal from int":
let s = 123456
var d = newDecimal(s)
let correct = "123456"
check $d == correct
test "Decimal Addition":
var a = newDecimal("1.2")
var b = newDecimal("3.5")
var c = a + b
let correct = "4.7"
check $c == correct
test "Decimal Subtraction":
var a = newDecimal("1.2")
var b = newDecimal("3.5")
var c = a - b
let correct = "-2.3"
check $c == correct
test "Decimal Multiplication":
var a = newDecimal("1.2")
var b = newDecimal("3.5")
var c = a * b
let correct = "4.20"
check $c == correct
test "Decimal Division":
var a = newDecimal("6.25")
var b = newDecimal("2.5")
var c = a / b
let correct = "2.5"
check $c == correct