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)) }) }
//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) }
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) }
//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) }
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)) }
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 }) }
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 }
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) }) }