Skip to content

Instantly share code, notes, and snippets.

@maddyblue
Created December 15, 2016 21:51
Show Gist options
  • Save maddyblue/009bcee8f0ef790808411d01a0b4175c to your computer and use it in GitHub Desktop.
Save maddyblue/009bcee8f0ef790808411d01a0b4175c to your computer and use it in GitHub Desktop.
postgres v crdb float checker
package main
import (
"database/sql"
"fmt"
"log"
"math"
"math/rand"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
_ "github.com/lib/pq"
)
func main() {
r, _ := randutil.NewPseudoRand()
pg, err := sql.Open("postgres", "user=postgres sslmode=disable")
if err != nil {
log.Fatal(err)
}
cr, err := sql.Open("postgres", "port=26257 sslmode=disable")
if err != nil {
log.Fatal(err)
}
dbs := []*sql.DB{pg, cr}
res := make([]string, len(dbs))
const query = "SELECT ln(%f)"
for {
q := fmt.Sprintf("SELECT ln(%v::decimal)", Float64(r))
for i, db := range dbs {
var s string
if err := db.QueryRow(q).Scan(&s); err != nil {
panic(fmt.Errorf("%s: %s", q, err))
}
res[i] = s
}
sd := sameDigits(res[0], res[1])
if sd < 12 {
fmt.Printf("%s\n\t%v\n\t%v (pg)\n\t%v (cr)\n", q, sd, res[0], res[1])
}
}
}
func sameDigits(a, b string) int {
s := 0
m := 0
for s < len(a) && s < len(b) && a[s] == b[s] {
switch a[s] {
case '-', '.':
m++
}
s++
}
return s - m
}
func Float64(r *rand.Rand) float64 {
v := r.Float64()
switch r.Intn(3) {
case 0:
i := r.Intn(75)
v *= math.Pow10(i)
case 1:
i := r.Intn(50)
v *= math.Pow10(-i)
case 2:
// ignore
default:
panic("nope")
}
return v
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment