Resources Numerical accuracy Calculation policy

Number conversion in LISP variants

Converts the value of a variant to another type when a LISP program asks for it.

How the precision is kept

Numbers in the interpreter are 64-bit, and a variant of type single or double holds the same 64-bit value. Conversion to a whole number rounds; conversion from text reads the leading number. The rest of this file passes values between LISP and the drawing without changing them.

Calculations under this policy

1 calculation in 1 source file. The code is read from the released app when this page loads. It is the calculation people run, not a copy kept for this page.

lisp/vla.js CadCraft 0.8.2 64-bit 1 calculation Reviewed

Tests None found by the review

Variant changed to another type

64-bit

The value of a variant as a string, a double, a single, an integer, a long or a boolean.

integer = round(n); double = n

A variant converted to the single type keeps its full 64-bit value; it is not narrowed to 32 bits. Conversion to integer or long rounds halves toward positive infinity and does not check the 16-bit or 32-bit range. A string that does not start with a number gives nil.

  • lisp/vla.js
  • lines 161 to 170
  • CadCraft 0.8.2
d('VLAX-VARIANT-CHANGE-TYPE', (a) => {
  arity(a, 2);
  if (!(a[0] instanceof Variant)) throw new LispError('bad argument type: variantp ' + show(a[0]));
  const to = intv(a[1]), v = a[0].value;
  if (to === VT.STRING) return new Variant(to, isNum(v) ? (isInt(v) ? String(v.v) : String(v)) : show(v, false));
  if (to === VT.DOUBLE || to === VT.SINGLE) { const n = isNum(v) ? numVal(v) : typeof v === 'string' ? parseFloat(v) : NaN; return Number.isFinite(n) ? new Variant(to, n) : null; }
  if (to === VT.INTEGER || to === VT.LONG) { const n = isNum(v) ? numVal(v) : typeof v === 'string' ? parseFloat(v) : NaN; return Number.isFinite(n) ? new Variant(to, int(Math.round(n))) : null; }
  if (to === VT.BOOLEAN) return new Variant(to, truthy(v) && v !== FALSE && !(isNum(v) && numVal(v) === 0) ? TRUE : FALSE);
  return new Variant(to, v);
});

In detail

Method
To double or single: the number itself, or parseFloat of a string. To integer or long: Math.round of that. To string: the number's shortest decimal form.
Number types
64-bit numbers; whole-number variants hold the interpreter's integer type.
Limits
Math.round rounds a half toward positive infinity (2.5 → 3, −2.5 → −2). parseFloat ignores text after the leading number.

Approach: 64-bit, direct

Every value is a 64-bit number from input to result. Formulas are closed form or a direct solve, and every comparison uses a stated tolerance.

Every policy that takes this approach