Example #1
0
func parseIntExpressions(s string, starti int) []pElement {
	ret := make([]pElement, 0)
	ind := intRE.FindStringIndex(s)
	if len(ind) == 0 {
		return ret
	} else {
		if ind[0] < ind[1] {
			var exp *pcalc.Expression
			in, err := strconv.ParseInt(s[ind[0]:ind[1]], 10, 64)
			if err != nil {
				fmt.Println(err)
				exp = nil
			} else {
				num := pcalc.MakeFraction(in, 1)
				exp = pcalc.NewExpressionWithConstant(num)
			}
			ne := pElement{exp, [2]int{starti + ind[0], starti + ind[1]}}
			ret = append(ret, ne)
		}
		if ind[1] < len(s) {
			ret = append(ret, parseIntExpressions(s[ind[1]:], starti+ind[1])...)
		}
	}
	return ret
}
Example #2
0
func parseFloatExpressions(s string, starti int) []pElement {
	nextParseFunction := parseIntExpressions
	ret := make([]pElement, 0)
	ind := sdFloatRE.FindStringIndex(s)
	if len(ind) == 0 {
		ret = nextParseFunction(s, starti)
	} else {
		if ind[0] > 0 {
			ret = append(ret, nextParseFunction(s[:ind[0]], starti)...)
		}
		if ind[0] < ind[1] {
			var exp *pcalc.Expression
			in, err := strconv.ParseFloat(s[ind[0]:ind[1]], 64)
			if err != nil {
				fmt.Println(err)
				exp = nil
			} else {
				num := pcalc.MakeSDFloat(in, uint8(ind[1]-ind[0]-1))
				exp = pcalc.NewExpressionWithConstant(num)
			}
			ne := pElement{exp, [2]int{starti + ind[0], starti + ind[1]}}
			ret = append(ret, ne)
		}
		if ind[1] < len(s) {
			ret = append(ret, parseFloatExpressions(s[ind[1]:], starti+ind[1])...)
		}
	}
	return ret
}
Example #3
0
func setupConstantExps() {
	standardFuncs["ln"] = pcalc.NaturalLogarithmOfExpression(pcalc.NewExpressionWithUnknown("a"))
	standardFuncs["lg"] = pcalc.DecimalLogarithmOfExpression(pcalc.NewExpressionWithUnknown("a"))

	chemicalConstants["R"] = pcalc.NewExpressionWithConstant(pcalc.MakeSDFloat(8.3144, 5))
	chemicalConstants["r"] = chemicalConstants["R"]
	chemicalConstants["F"] = pcalc.NewExpressionWithConstant(pcalc.MakeSDFloat(96485.33, 7))
	chemicalConstants["f"] = chemicalConstants["F"]

	physicalConstants["C"] = pcalc.NewExpressionWithConstant(pcalc.MakeSDFloat(299792458, 9))
	physicalConstants["c"] = physicalConstants["C"]
	physicalConstants["G"] = pcalc.NewExpressionWithConstant(pcalc.MakeSDFloat(9.80665, 6))
	physicalConstants["g"] = physicalConstants["G"]

	mathematicalConstants["pi"] = pcalc.NewExpressionWithConstant(pcalc.MakeSDFloat(math.Pi, 15))
	mathematicalConstants["e"] = pcalc.NewExpressionWithConstant(pcalc.MakeSDFloat(math.E, 15))
}