From ec6a3ba492cc68cd2ee6c4fbd0d70b471bf01237 Mon Sep 17 00:00:00 2001 From: Benjamin VAUDOUR Date: Sun, 25 Feb 2024 14:30:50 +0100 Subject: [PATCH] Corrections diverses du package number + Ajout de stringers pour opition.Option et option.Choice --- number/number.go | 33 ++++++++++++++++++++++++--------- number/operations.go | 5 ----- option/choice.go | 17 +++++++++++++++++ option/option.go | 11 +++++++++++ 4 files changed, 52 insertions(+), 14 deletions(-) diff --git a/number/number.go b/number/number.go index fc36be5..37483c2 100644 --- a/number/number.go +++ b/number/number.go @@ -146,7 +146,7 @@ func (n Number) IsUint64() bool { // Is retourne vrai si n = i. func (n Number) Is(i int64) bool { - if nb, ok := n.get(); ok && nb.IsInt() { + if nb, ok := n.rat(); ok && nb.IsInt() { num := nb.Num() return num.IsInt64() && num.Int64() == i } @@ -156,7 +156,7 @@ func (n Number) Is(i int64) bool { // IsUint retourne vrai si n = u. func (n Number) IsUint(u uint64) bool { - if nb, ok := n.get(); ok && nb.IsInt() { + if nb, ok := n.rat(); ok && nb.IsInt() { num := nb.Num() return num.IsUint64() && num.Uint64() == u } @@ -166,7 +166,7 @@ func (n Number) IsUint(u uint64) bool { // IsFloat retourne vrai si n = f. func (n Number) IsFloat(f float64) bool { - if nb, ok := n.get(); ok { + if nb, ok := n.rat(); ok { nf, ok := nb.Float64() return nf == f && ok } @@ -195,7 +195,7 @@ func (n Number) IsNan() bool { return !n.IsDefined() && n.Sign() == 0 } // ToInt64 retourne le nombre converti en int64, et vrai si la conversion a réussi. func (n Number) ToInt64() (i int64, ok bool) { var nb *big.Rat - if nb, ok = n.get(); ok { + if nb, ok = n.rat(); ok { if ok = nb.IsInt(); ok { num := nb.Num() if ok = num.IsInt64(); ok { @@ -210,7 +210,7 @@ func (n Number) ToInt64() (i int64, ok bool) { // ToUint64 retourne le nombre converti en uint64, et vrai si la conversion a réussi. func (n Number) ToUint64() (u uint64, ok bool) { var nb *big.Rat - if nb, ok = n.get(); ok { + if nb, ok = n.rat(); ok { if ok = nb.IsInt(); ok { num := nb.Num() if ok = num.IsUint64(); ok { @@ -222,6 +222,16 @@ func (n Number) ToUint64() (u uint64, ok bool) { return } +// ToFloat retourne le nombre converti en flottant, et vrai si la conversion a réussi et est exacte. +func (n Number) ToFloat() (f float64, ok bool) { + var nb *big.Rat + if nb, ok = n.rat(); ok { + f, ok = nb.Float64() + } + + return +} + // IsEven retourne vrai si n est entier et pair. func (n Number) IsEven() bool { if nb, ok := n.get(); ok { @@ -611,7 +621,7 @@ func (n Number) Fact() Number { default: out := One(n.Base()) for e := Two(); e.Le(n); e = e.Inc() { - out.Mul(e) + out = out.Mul(e) } return out } @@ -672,14 +682,19 @@ func (n Number) decString() (out string) { dot := len(out) - p out = fmt.Sprintf("%s.%s", out[:dot], out[dot:]) + for out[0] == '0' { + out = out[1:] + } + if !FixedPrecision { l := len(out) - 1 for out[l] == '0' { out, l = out[:l], l-1 } - if out == "." { - out = "0." - } + } + + if out == "." { + out = "0." } if sign < 0 { diff --git a/number/operations.go b/number/operations.go index a30f415..cc70115 100644 --- a/number/operations.go +++ b/number/operations.go @@ -144,11 +144,6 @@ func Median(numbers ...Number) Number { } numbers = Sort(numbers...) - if l&1 == 0 { - i := l >> 1 - return numbers[i].Add(numbers[i-1]).Div(Two()) - } - return numbers[l>>1] } diff --git a/option/choice.go b/option/choice.go index 9aaa8cc..19182a8 100644 --- a/option/choice.go +++ b/option/choice.go @@ -1,5 +1,9 @@ package option +import ( + "fmt" +) + // Option permet de définir des valeurs alternative. type Choice[T1 any, T2 any] struct { left Option[T1] @@ -44,3 +48,16 @@ func (c Choice[T1, T2]) IsRight() bool { func (c Choice[T1, T2]) IsNil() bool { return !c.IsLeft() && !c.IsRight() } + +func (c Choice[T1, T2]) String() string { + return fmt.Sprintf(`{ + left: { + value: %v, + ok: %v, + }, + right: { + value: %v, + ok: %v, + }, +}`, c.left.v, c.left.ok, c.right.v, c.right.ok) +} diff --git a/option/option.go b/option/option.go index ebce66e..87d25eb 100644 --- a/option/option.go +++ b/option/option.go @@ -1,5 +1,9 @@ package option +import ( + "fmt" +) + // Option permet de définir des valeurs optionnelles, afin de se passer de nil. type Option[T any] struct { v T @@ -29,3 +33,10 @@ func (o Option[T]) Get() (v T, ok bool) { func (o Option[T]) IsDefined() bool { return o.ok } + +func (o Option[T]) String() string { + return fmt.Sprintf(`{ + value: %v, + ok: %v, +}`, o.v, o.ok) +}