Skip to content

Commit

Permalink
clean up SetString (#168)
Browse files Browse the repository at this point in the history
Updates #166

Signed-off-by: Eric Lagergren <[email protected]>
  • Loading branch information
ericlagergren authored Oct 22, 2021
1 parent e27628d commit f12e1f0
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 282 deletions.
23 changes: 14 additions & 9 deletions big.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func (z *Big) xflow(exp int, over, neg bool) *Big {

func (x *Big) isCompact() bool { return x.compact != c.Inflated }
func (x *Big) isInflated() bool { return !x.isCompact() }
func (x *Big) isSpecial() bool { return x.form&(inf|nan) != 0 }
func (x *Big) isSpecial() bool { return x.form&special != 0 }

// isZero reports whether x is zero.
//
Expand Down Expand Up @@ -1641,10 +1641,9 @@ func (z *Big) SetScale(scale int) *Big {
// can be passed to SetString.
var Regexp = regexp.MustCompile(`(?i)(([+-]?(\d+\.\d*|\.?\d+)([eE][+-]?\d+)?)|(inf(infinity)?))|([+-]?([sq]?nan\d*))`)

// SetString sets z to the value of s, returning z and a bool
// indicating success.
// SetString sets z to the value of s and returns z.
//
// s must be a string in one of the following formats:
// s must have one of the following formats:
//
// 1.234
// 1234
Expand All @@ -1656,11 +1655,17 @@ var Regexp = regexp.MustCompile(`(?i)(([+-]?(\d+\.\d*|\.?\d+)([eE][+-]?\d+)?)|(i
// qNaN
// sNaN
//
// Each value may be preceded by an optional sign, "-" or "+".
// "Inf" and "NaN" map to "+Inf" and "qNaN", respectively. NaN
// values may have optional diagnostic information, represented
// as trailing digits; for example, "NaN123". These digits are
// otherwise ignored but are included for robustness.
// Each format may be preceded by an optional sign, either "-" or
// "+". By default, "Inf" and "NaN" map to "+Inf" and "qNaN",
// respectively. NaN values may have optional diagnostic
// information, represented as trailing digits; for example,
// "NaN123".
//
// If s does not match one of the allowed formats, the
// ConversionSyntax condition is set.
//
// SetString will only return (nil, false) if a library error
// occurs. In general, it safe to ignore the bool result.
func (z *Big) SetString(s string) (*Big, bool) {
if err := z.scan(strings.NewReader(s)); err != nil {
return nil, false
Expand Down
22 changes: 10 additions & 12 deletions big_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,12 +650,10 @@ func TestExp(t *testing.T) {
{"8.5", "4914.768840299134375431373676347828585214799657921037598580285209797607458733169528371064981979491498670292533027962716306703436339854535231155132783610120401650762002683099021054602507219969322181366306285407097420610361642274475387237868040396608369229178064395794276613661196368533479681502003855009", 301, Inexact | Rounded},
{"-3.2111455189183169875200429E-37", "1.0000000000000000000000000", 26, Inexact | Rounded},
} {
var x Big
var z, x, r Big
x.SetString(tc.x)
var z Big
ctx := Context{Precision: tc.prec}
ctx.Exp(&z, &x)
var r Big
r.Context.Conditions |= tc.cond
r.SetString(tc.r)
if !equal(&r, &z) {
Expand Down Expand Up @@ -1005,14 +1003,14 @@ func TestBig_Round(t *testing.T) {
to int
res string
}{
0: {"5.5", 1, "6"},
1: {"1.234", 2, "1.2"},
2: {"1", 1, "1"},
3: {"9.876", 0, "9.876"},
4: {"5.65", 2, "5.6"},
5: {"5.0002", 2, "5"},
6: {"0.000158674", 6, "0.000158674"},
7: {"1.58089722856961873690377135139876745465351534188711107066818e+12288", 50, "1.5808972285696187369037713513987674546535153418871e+12288"},
{"5.5", 1, "6"},
{"1.234", 2, "1.2"},
{"1", 1, "1"},
{"9.876", 0, "9.876"},
{"5.65", 2, "5.6"},
{"5.0002", 2, "5"},
{"0.000158674", 6, "0.000158674"},
{"1.58089722856961873690377135139876745465351534188711107066818e+12288", 50, "1.5808972285696187369037713513987674546535153418871e+12288"},
} {
bd, _ := new(Big).SetString(test.v)
r, _ := new(Big).SetString(test.res)
Expand All @@ -1026,7 +1024,7 @@ got : %q
}

func TestBig_Scan(t *testing.T) {
// TODO(eric): write this test
// TODO(eric): this
}

func TestBig_SetFloat64(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ type Context struct {
//
Precision int

// Traps are a set of exceptional conditions that should result in an error.
// Traps are a set of exceptional conditions that should
// result in an error.
Traps Condition

// Conditions are a set of the most recent exceptional conditions to occur
// during an operation.
// Conditions are a set of the most recent exceptional
// conditions to occur during an operation.
Conditions Condition

// RoundingMode determines how a decimal is rounded.
Expand Down
4 changes: 2 additions & 2 deletions pytables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (c *scase) Assert(got, want interface{}) {
c.t.Helper()

if !reflect.DeepEqual(got, want) {
c.t.Fatalf(`#%d: %s
c.t.Errorf(`#%d: %s
wanted: %v
got : %v
`, c.i, c.c.ShortString(22), want, got)
Expand All @@ -276,7 +276,7 @@ func (c *scase) Check(z *Big) {

r := c.R()
if !pytablesEqual(z, r) {
c.t.Fatalf(`#%d: %s
c.t.Errorf(`#%d: %s
wanted: %q (%s:%d)
got : %q (%s:%d)
`,
Expand Down
Loading

0 comments on commit f12e1f0

Please sign in to comment.