示例#1
0
文件: math.go 项目: catb0t/gelo
func Abs(vm *gelo.VM, args *gelo.List, ac uint) gelo.Word {
	if ac == 0 {
		return gelo.NewNumber(0)
	}
	return args.MapOrApply(func(w gelo.Word) gelo.Word {
		n := vm.API.NumberOrElse(w).Real()
		return gelo.NewNumber(math.Abs(n))
	})
}
示例#2
0
文件: math.go 项目: catb0t/gelo
//Left-to-right
func Difference(vm *gelo.VM, args *gelo.List, ac uint) gelo.Word {
	if ac == 0 {
		return gelo.NewNumber(0)
	}
	acc := vm.API.NumberOrElse(args.Value).Real()
	for args = args.Next; args != nil; args = args.Next {
		acc -= vm.API.NumberOrElse(args.Value).Real()
	}
	return gelo.NewNumber(acc)
}
示例#3
0
文件: math.go 项目: catb0t/gelo
func Product(vm *gelo.VM, args *gelo.List, ac uint) gelo.Word {
	if ac == 0 {
		return gelo.NewNumber(0)
	}
	var acc float64 = 1
	for ; args != nil; args = args.Next {
		acc *= vm.API.NumberOrElse(args.Value).Real()
	}
	return gelo.NewNumber(acc)
}
示例#4
0
文件: math.go 项目: catb0t/gelo
//Left-to-right
func Quotient(vm *gelo.VM, args *gelo.List, ac uint) gelo.Word {
	if ac == 0 {
		return gelo.NewNumber(0)
	}
	acc := vm.API.NumberOrElse(args.Value).Real()
	for args = args.Next; args != nil; args = args.Next {
		n := vm.API.NumberOrElse(args.Value).Real()
		if n == 0 {
			gelo.RuntimeError(vm, "Division by 0")
		}
		acc /= n
	}
	return gelo.NewNumber(acc)
}
示例#5
0
文件: math.go 项目: catb0t/gelo
func Mod(vm *gelo.VM, args *gelo.List, ac uint) gelo.Word {
	if ac != 2 {
		gelo.ArgumentError(vm, "mod", "number base", args)
	}
	n := vm.API.NumberOrElse(args.Value).Real()
	m := vm.API.NumberOrElse(args.Next.Value).Real()
	return gelo.NewNumber(math.Mod(n, m))
}
示例#6
0
文件: math.go 项目: catb0t/gelo
func NumberCon(vm *gelo.VM, args *gelo.List, ac uint) gelo.Word {
	if ac == 0 {
		return gelo.NewNumber(0)
	}
	return args.MapOrApply(func(w gelo.Word) gelo.Word {
		if n, ok := w.(*gelo.Number); ok {
			return n
		}
		if n, ok := gelo.NewNumberFromBytes(w.Ser().Bytes()); ok {
			return n
		}
		return gelo.False
	})
}
示例#7
0
文件: math.go 项目: catb0t/gelo
func Decrx(vm *gelo.VM, args *gelo.List, ac uint) gelo.Word {
	if ac != 1 {
		gelo.ArgumentError(vm, "decr!", "reference to number", args)
	}
	n, ok := vm.Ns.MutateBy(args.Value,
		func(w gelo.Word) (gelo.Word, bool) {
			n := vm.API.NumberOrElse(w)
			return gelo.NewNumber(n.Real() - 1), true
		})
	if !ok {
		gelo.VariableUndefined(vm, args.Value)
	}
	return n
}
示例#8
0
文件: math.go 项目: catb0t/gelo
func Sgn(vm *gelo.VM, args *gelo.List, ac uint) gelo.Word {
	if ac == 0 {
		gelo.ArgumentError(vm, "sgn", "number+", "")
	}
	return args.MapOrApply(func(w gelo.Word) gelo.Word {
		n := vm.API.NumberOrElse(w).Real()
		switch {
		case n < 0:
			n = -1
		case n > 0:
			n = 1
		case n == 0:
			n = 0
		}
		return gelo.NewNumber(n)
	})
}