UNPKG

godprotocol

Version:

A distributed computing environment for Web 4.0 — integrating AI, decentralisation, and virtual computation.

221 lines (175 loc) 6.05 kB
import Storage from "./functions/storage.js"; class Num extends Storage { constructor(config, account) { super(account) this.config = config this.type = 'number' this.methods = { ...this.methods, 'inc': {args: [['other', 'number']], ret: ['result', 'number']}, 'dec': {args: [['other', 'number']], ret: ['result', 'number']}, '__mod_': {args: [['other', 'number']], ret: ['result', 'number']}, '__exp__': {args: [['other', 'number']], ret: ['result', 'number']}, '__floordiv__': {args: [['other', 'number']], ret: ['result', 'number']}, 'to_degrees': {args: [], ret: ['result', 'number']}, 'to_radians': {args: [], ret: ['result', 'number']}, 'to_string': {args: [], ret: ['result', 'number']}, 'to_float': {args: [], ret: ['result', 'number']}, 'to_int': {args: [], ret: ['result', 'number']}, 'sqrt': {args: [], ret: ['result', 'number']}, 'abs': {args: [], ret: ['result', 'number']}, 'sin': {args: [], ret: ['result', 'number']}, 'tan': {args: [], ret: ['result', 'number']}, 'cos': {args: [], ret: ['result', 'number']}, 'asin': {args: [], ret: ['result', 'number']}, 'acos': {args: [], ret: ['result', 'number']}, 'atan': {args: [], ret: ['result', 'number']}, 'atan2': {args: [], ret: ['result', 'number']}, 'tanh': {args: [], ret: ['result', 'number']}, 'sinh': {args: [], ret: ['result', 'number']}, 'cosh': {args: [], ret: ['result', 'number']}, 'acosh': {args: [], ret: ['result', 'number']}, 'asinh': {args: [], ret: ['result', 'number']}, 'atanh': {args: [], ret: ['result', 'number']}, 'floor': {args: [], ret: ['result', 'number']}, 'ceil': {args: [], ret: ['result', 'number']}, 'round': {args: [['other', 'number']], ret: ['result', 'number']}, 'is_prime': {args: [], ret: ['result', 'boolean']}, 'is_perfect_square': {args: [], ret: ['result', 'boolean']}, 'log': {args: [['other', 'number']], ret: ['result', 'number']}, 'is_even': {args: [], ret: ['result', 'boolean']}, 'is_odd': {args: [], ret: ['result', 'boolean']}, 'factorial': {args: [['factor', 'number']], ret: ['result', 'number']}, } } __or__ = other=>this.value || other __and__ = other=>this.value && other __add__ = (other)=>{ return this.value + other } inc = async(other)=>{ other = other || 1 let val = this.value + other; let {vm, chain} = this.account return await vm.instantiate({ type: 'number', value: val.toString(), location: this.address }, {chain, _id: this.config._id}) } dec = async(other)=>{ other = other || 1 let val = this.value - other; let {vm, chain} = this.account return await vm.instantiate({ type: 'number', value: val.toString(), location: this.address }, {chain, _id: this.config._id}) } __mul__ = (other)=>{ return this.value * other } __sub__ = (other)=>{ return this.value - other } __div__ = async (other)=>{ if(other === 0){ return await this.account.vm.throw_error({type:"ZeroDivisionError", message: "Cannot have 0 be a denominator"}) }else return this.value / other } __gt__ = (other)=> this.value > other __lt__ = (other)=> this.value < other __gte__ = (other)=> this.value >= other __lte__ = (other)=> this.value <= other __ne__ = (other)=> this.value != other __eq__ = (other)=> this.value == other __mod__ = (other)=> this.value % other __exp__ = (other)=> this.value ** other __floordiv__ = (other)=> Math.floor(this.value / other) to_degrees = ()=>{ return this.value * (180 / Math.PI) } to_radians = ()=>{ return this.value * (Math.PI / 180) } to_string = ()=>{ return this.value.toString() } to_float = (other)=>{ return this.value.toFixed(other) } to_int = ()=>parseInt(this.value) sqrt = ()=>Math.sqrt(this.value) abs = ()=>Math.abs(this.value) sin = ()=>Math.sin(this.value) tan = ()=>Math.tan(this.value) cos = ()=>Math.cos(this.value) asin = ()=>Math.asin(this.value) acos = ()=>Math.acos(this.value) atan = ()=>Math.atan(this.value) atan2 = ()=>Math.atan2(this.value) tanh = ()=>Math.tanh(this.value) sinh = ()=>Math.sinh(this.value) cosh = ()=>Math.cosh(this.value) asinh = ()=>Math.asinh(this.value) acosh = ()=>Math.acosh(this.value) atanh = ()=>Math.atanh(this.value) floor = ()=>Math.floor(this.value) ceil = ()=>Math.ceil(this.value) round = (other)=>{ let result; if (other) { result = Number(Number(this.value).toFixed(other)); } else result = Math.round(this.value); return result; } is_prime = ()=>{ let result = true; if (this.value <= 1) result = false; if (this.value <= 3) result = true; if (this.value % 2 === 0 || this.value % 3 === 0) result = false; for (let i = 5; i * i <= this.value; i += 6) { if (this.value % i === 0 || this.value % (i + 2) === 0) { result = false; } } return result; } is_perfect_square = ()=>{ let result; if (this.value < 0) result = false; let sqrt = Math.sqrt(this.value); result = sqrt === Math.floor(sqrt); return result; } log = (other)=>{ let result; if (!other || other === 10) result = Math.log10(this.value); else if (other === Math.E) { result = Math.log(this.value); } else result = Math.log(this.value) / Math.log(other); return result } is_even = ()=>{ return this.value % 2 === 0 } is_odd = ()=>!this.is_even() literal = async()=>{ return Number(this.config.value) } factorial = (value)=>{ if (value == null) value = this.value; if (value === 0) return 1 return value * this.factorial(value - 1) } configs = (name)=>{ let conf = {__add__: { parameters: [{name: 'other', position: 0}] }} return conf[name] } } export default Num;