working on number type

This commit is contained in:
2022-11-22 17:50:23 +00:00
parent 1e091437fd
commit 7b09133438
5 changed files with 50 additions and 1 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@@ -5,7 +5,8 @@
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"build": "mkdir -p build && go build -o build/argon ./src", "build": "mkdir -p build && go build -o build/argon ./src",
"dev": "nodemon -x go run ./src/. --signal SIGKILL -e go --verbose" "dev": "nodemon -x go run ./src/. --signal SIGKILL -e go --verbose",
"start": "go run ./src/."
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@@ -3,5 +3,6 @@ package main
import "fmt" import "fmt"
func main() { func main() {
translate("")
fmt.Println("hello world") fmt.Println("hello world")
} }

31
src/number.go Normal file
View File

@@ -0,0 +1,31 @@
package main
import (
"fmt"
"math/big"
)
// a number type
type number = *big.Rat
// create a number from two integers
var createNumber = big.NewRat
// create a new number type
func newNumber() *big.Rat {
return new(big.Rat)
}
// converts a string into a number
func stringToNumber(str string) (*big.Rat, bool) {
return newNumber().SetString(str)
}
// converts a number type to a string
func numberToString(num number, fraction bool) string {
if fraction {
return num.RatString()
}
x, _ := num.Float64()
return fmt.Sprint(x)
}

16
src/translate.go Normal file
View File

@@ -0,0 +1,16 @@
package main
import (
"fmt"
)
func translate(code string) {
a := createNumber(7, 1)
b, worked := stringToNumber("1e-1")
if worked {
output := newNumber().Mul(a, b)
output.Add(output, a)
output.Sub(output, a)
fmt.Println(numberToString(output, true))
}
}