# Why is this usage of math/big.Float.SetString no bug?

**URL:** <https://forum.golangbridge.org/t/why-is-this-usage-of-math-big-float-setstring-no-bug/41593>\
**Category:** Technical Discussion\
**Created:** [February 10, 2026, 12:30pm UTC](https://forum.golangbridge.org/t/why-is-this-usage-of-math-big-float-setstring-no-bug/41593 "2026-02-10T12:30:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ceving](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/ceving/32/17413_2.png) [@ceving](https://forum.golangbridge.org/u/ceving)\
**Post date:** [February 10, 2026, 12:30pm UTC](https://forum.golangbridge.org/t/why-is-this-usage-of-math-big-float-setstring-no-bug/41593/1 "2026-02-10T12:30:32Z")

</div>

The following prints false:

```go
package main

import (
	"fmt"
	"math/big"
)

func main() {
	r1 := big.NewRat(1, 10)
	f1 := big.NewFloat(0.1)
	q1 := new(big.Float).SetPrec(f1.Prec() + 1).SetRat(r1)
	c1 := q1.Cmp(f1)
	fmt.Println(c1)

	r2, ok := new(big.Rat).SetString("1/10")
	if !ok {
		panic("1/10")
	}
	f2, ok := new(big.Float).SetString("0.1")
	if !ok {
		panic("0.1")
	}
	q2 := new(big.Float).SetPrec(f2.Prec() + 1).SetRat(r2)
	c2 := q2.Cmp(f2)
	fmt.Println(c2)

	fmt.Println(c1 == c2)
}

```

Why is it no bug?

I think the rational number 1/10 should be always less than the floating point number 0.1.

---

<div class="post-metadata">

**Author:** ![falco467](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/falco467/32/13340_2.png) [@falco467](https://forum.golangbridge.org/u/falco467)\
**Post date:** [February 17, 2026, 10:43am UTC](https://forum.golangbridge.org/t/why-is-this-usage-of-math-big-float-setstring-no-bug/41593/2 "2026-02-17T10:43:15Z")

</div>

The rational number `1/10` exactly represents the mathematical number of one divided by ten. All floating point numbers are binary representations of a number, which is as close as possible to `1/10`

The binary representation of `1/10` is periodic: `0.0 0011 0011 0011 0011 0011 ....`

By setting the precision for a float you essentially decide where to cut off and round this infinitely long number. You will never get an exact representation of `1/10` because that would take a precision of infinity - rather you will get a number which is a bit smaller or a bit bigger, depending on the exact precision used. If you cut after a `0` the result will always be rounded down (simply truncated) and is smaller than `1/10`. But if you cut after a 1 bit, then depending on the following bits the number might be rounded up (increased by 1 after truncating) and result in a number bigger than `1/10`

You can try this by setting the precision to a manual value and try different precisions

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [May 18, 2026, 10:43am UTC](https://forum.golangbridge.org/t/why-is-this-usage-of-math-big-float-setstring-no-bug/41593/3 "2026-05-18T10:43:54Z")

</div>

Closed on May 18, 2026, 10:43am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
