Correction du module number
This commit is contained in:
parent
5b437644bd
commit
eff6ae9d7e
|
@ -11,16 +11,16 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
rSign = `(\+|-)`
|
rSign = `\+|-`
|
||||||
rBSign = `(0|1)`
|
rBSign = `(0|1)`
|
||||||
rNb2 = `(0|1)`
|
rNb2 = `(0|1)`
|
||||||
rNb8 = `[0-7]`
|
rNb8 = `[0-7]`
|
||||||
rNb10 = `\d`
|
rNb10 = `\d`
|
||||||
rNb16 = `[0-9a-fA-F]`
|
rNb16 = `[0-9a-fA-F]`
|
||||||
rNbN = `[0-9a-zA-Z]`
|
rNbN = `[0-9a-zA-Z]`
|
||||||
rBase2 = `(B|b)`
|
rBase2 = `B|b`
|
||||||
rBase8 = `(O|o)`
|
rBase8 = `O|o`
|
||||||
rBase16 = `(X|x)`
|
rBase16 = `X|x`
|
||||||
rExp10 = `E|e`
|
rExp10 = `E|e`
|
||||||
rExpN = `×\d+\^`
|
rExpN = `×\d+\^`
|
||||||
)
|
)
|
||||||
|
@ -28,23 +28,23 @@ const (
|
||||||
var (
|
var (
|
||||||
rInt2 = fmt.Sprintf(`(%s)%s+`, rBase2, rNb2)
|
rInt2 = fmt.Sprintf(`(%s)%s+`, rBase2, rNb2)
|
||||||
rInt8 = fmt.Sprintf(`(%s)%s+`, rBase8, rNb8)
|
rInt8 = fmt.Sprintf(`(%s)%s+`, rBase8, rNb8)
|
||||||
rInt10 = fmt.Sprintf(`(%s)%s+`, rSign, rNb10)
|
rInt10 = fmt.Sprintf(`(%s)?%s+`, rSign, rNb10)
|
||||||
rInt16 = fmt.Sprintf(`(%s)%s+`, rBase8, rNb8)
|
rInt16 = fmt.Sprintf(`(%s)%s+`, rBase16, rNb16)
|
||||||
rIntN = fmt.Sprintf(`\(%s+\)%s?%s+`, rNb10, rSign, rNbN)
|
rIntN = fmt.Sprintf(`\(%s+\)(%s)?%s+`, rNb10, rSign, rNbN)
|
||||||
rIntB = fmt.Sprintf(`%s(%s|%s|%s)`, rBSign, rInt2, rInt8, rInt16)
|
rIntB = fmt.Sprintf(`%s(%s|%s|%s)`, rBSign, rInt2, rInt8, rInt16)
|
||||||
rInt = fmt.Sprintf(`(%s|%s|%s)`, rInt10, rIntN, rIntB)
|
rInt = fmt.Sprintf(`(%s|%s|%s)`, rInt10, rIntN, rIntB)
|
||||||
|
|
||||||
rDec2 = fmt.Sprintf(`(%s)(%s+\.%s*|\.%s+)`, rBase2, rNb2, rNb2, rNb2)
|
rDec2 = fmt.Sprintf(`(%s)(%s+\.%s*|\.%s+)`, rBase2, rNb2, rNb2, rNb2)
|
||||||
rDec8 = fmt.Sprintf(`(%s)(%s+\.%s*|\.%s+)`, rBase8, rNb8, rNb8, rNb8)
|
rDec8 = fmt.Sprintf(`(%s)(%s+\.%s*|\.%s+)`, rBase8, rNb8, rNb8, rNb8)
|
||||||
rDec10 = fmt.Sprintf(`%s?(%s+\.%s*|\.%s+)`, rSign, rNb10, rNb10, rNb10)
|
rDec10 = fmt.Sprintf(`(%s)?(%s+\.%s*|\.%s+)`, rSign, rNb10, rNb10, rNb10)
|
||||||
rDec16 = fmt.Sprintf(`(%s)(%s+\.%s*|\.%s+)`, rBase16, rNb16, rNb16, rNb16)
|
rDec16 = fmt.Sprintf(`(%s)(%s+\.%s*|\.%s+)`, rBase16, rNb16, rNb16, rNb16)
|
||||||
rDecN = fmt.Sprintf(`\(%s+\)%s?(%s+\.%s*|\.%s+)`, rNb10, rSign, rNbN, rNbN, rNbN)
|
rDecN = fmt.Sprintf(`\(%s+\)(%s)?(%s+\.%s*|\.%s+)`, rNb10, rSign, rNbN, rNbN, rNbN)
|
||||||
rDecB = fmt.Sprintf(`%s(%s|%s|%s)`, rBSign, rDec2, rDec8, rDec16)
|
rDecB = fmt.Sprintf(`%s(%s|%s|%s)`, rBSign, rDec2, rDec8, rDec16)
|
||||||
rDec = fmt.Sprintf(`(%s|%s|%s)`, rDec10, rDecN, rDecB)
|
rDec = fmt.Sprintf(`(%s|%s|%s)`, rDec10, rDecN, rDecB)
|
||||||
rExponent10 = fmt.Sprintf(`(%s|%s)(%s)%s?%s+`, rInt10, rDec10, rExp10, rSign, rNb10)
|
rExponent10 = fmt.Sprintf(`(%s|%s)(%s)(%s)?%s+`, rInt10, rDec10, rExp10, rSign, rNb10)
|
||||||
rExponentN = fmt.Sprintf(`(%s|%s)%s%s?%s+`, rInt, rDec, rExpN, rSign, rNb10)
|
rExponentN = fmt.Sprintf(`(%s|%s)%s(%s)?%s+`, rInt, rDec, rExpN, rSign, rNb10)
|
||||||
rAll10 = fmt.Sprintf(`(%s|%s)((%s)%s?%s+)?`, rInt10, rDec10, regExp10, rSign, rNb10)
|
rAll10 = fmt.Sprintf(`(%s|%s)((%s)(%s)?%s+)?`, rInt10, rDec10, regExp10, rSign, rNb10)
|
||||||
rAllN = fmt.Sprintf(`(%s|%s)(%s%s?%s+)?`, rInt, rDec, rExpN, rSign, rNb10)
|
rAllN = fmt.Sprintf(`(%s|%s)(%s(%s)?%s+)?`, rInt, rDec, rExpN, rSign, rNb10)
|
||||||
rAll = fmt.Sprintf(`(%s|%s)`, rAll10, rAllN)
|
rAll = fmt.Sprintf(`(%s|%s)`, rAll10, rAllN)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ var (
|
||||||
regDec = regexp.MustCompile(fmt.Sprintf(`^%s$`, rDec))
|
regDec = regexp.MustCompile(fmt.Sprintf(`^%s$`, rDec))
|
||||||
|
|
||||||
regExp10 = regexp.MustCompile(fmt.Sprintf(`^%s$`, rExponent10))
|
regExp10 = regexp.MustCompile(fmt.Sprintf(`^%s$`, rExponent10))
|
||||||
regExpN = regexp.MustCompile(fmt.Sprintf(`^%s$`, rExpN))
|
regExpN = regexp.MustCompile(fmt.Sprintf(`^%s$`, rExponentN))
|
||||||
|
|
||||||
regFrac = regexp.MustCompile(fmt.Sprintf(`^%s/%s$`, rAll, rAll))
|
regFrac = regexp.MustCompile(fmt.Sprintf(`^%s/%s$`, rAll, rAll))
|
||||||
)
|
)
|
||||||
|
|
|
@ -233,7 +233,7 @@ func Parse(str string) (out option.Option[Number]) {
|
||||||
}
|
}
|
||||||
case regExpN.MatchString(str):
|
case regExpN.MatchString(str):
|
||||||
i, j := strings.Index(str, "×"), strings.Index(str, "^")
|
i, j := strings.Index(str, "×"), strings.Index(str, "^")
|
||||||
if expBase, err := strconv.ParseUint(str[i+1:j], 10, 64); err == nil && isBaseValid(expBase) {
|
if expBase, err := strconv.ParseUint(str[i+2:j], 10, 64); err == nil && isBaseValid(expBase) {
|
||||||
if exponent, err := strconv.Atoi(str[j+1:]); err == nil {
|
if exponent, err := strconv.Atoi(str[j+1:]); err == nil {
|
||||||
if n, ok := Parse(str[:i]).Get(); ok {
|
if n, ok := Parse(str[:i]).Get(); ok {
|
||||||
out = option.Some(n.Mul(pow(expBase, exponent)).ToType(Scientific))
|
out = option.Some(n.Mul(pow(expBase, exponent)).ToType(Scientific))
|
||||||
|
@ -242,7 +242,7 @@ func Parse(str string) (out option.Option[Number]) {
|
||||||
}
|
}
|
||||||
case regFrac.MatchString(str):
|
case regFrac.MatchString(str):
|
||||||
i := strings.Index(str, "/")
|
i := strings.Index(str, "/")
|
||||||
num, denom := Parse(str[:i]), Parse(str[i:])
|
num, denom := Parse(str[:i]), Parse(str[i+1:])
|
||||||
if n, ok := num.Get(); ok {
|
if n, ok := num.Get(); ok {
|
||||||
if d, ok := denom.Get(); ok {
|
if d, ok := denom.Get(); ok {
|
||||||
out = option.Some(n.Div(d).ToType(Fraction))
|
out = option.Some(n.Div(d).ToType(Fraction))
|
||||||
|
|
|
@ -31,10 +31,10 @@ func (n *Number) get() (r *big.Rat, ok bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Number) rat() (r *big.Rat, ok bool) {
|
func (n *Number) rat() (r *big.Rat, ok bool) {
|
||||||
if r, ok := n.get(); ok {
|
if r, ok = n.get(); ok {
|
||||||
r = new(big.Rat).Set(r)
|
r = new(big.Rat).Set(r)
|
||||||
if n.Sign() < 0 {
|
if n.Sign() < 0 {
|
||||||
r = r.Neg(r)
|
r.Neg(r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue