Skip to content

Instantly share code, notes, and snippets.

@konjoot
Last active January 4, 2018 09:47
Show Gist options
  • Save konjoot/d2dcda065d8218b19e75026479963ca5 to your computer and use it in GitHub Desktop.
Save konjoot/d2dcda065d8218b19e75026479963ca5 to your computer and use it in GitHub Desktop.
Print negative powers
package main
import (
"flag"
"fmt"
"os"
)
func main() {
var (
base int
power int
)
flag.IntVar(&base, "base", 2, "base")
flag.IntVar(&power, "power", 10, "max negative power")
flag.Parse()
numbers := [...]string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
d := make([]int, power)
d[0] = 1 / base
r := 1 % base
var i int
for k := 0; k < power; k++ {
fmt.Fprint(os.Stdout, "0.")
for i = 0; i < power; i++ {
r = 10*r + d[i]
d[i] = r / base
r = r % base
fmt.Fprint(os.Stdout, numbers[d[i]])
}
r = 0
fmt.Fprint(os.Stdout, "\n")
}
}
// go run main.go -base=3 -power=10
// 0.3333333333
// 0.1111111111
// 0.0370370370
// 0.0123456790
// 0.0041152263
// 0.0013717421
// 0.0004572473
// 0.0001524157
// 0.0000508052
// 0.0000169350
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment