optimized sqrt function

This commit is contained in:
Asaki Yuki 2026-02-13 12:41:47 +07:00
parent 796b89636d
commit ab83b33725
3 changed files with 70 additions and 38 deletions

View file

@ -49,20 +49,43 @@ export const defaultFunctions = {
}
},
sqrt: n => {
const notAllow = RandomBindingString()
const g = RandomBindingString()
const h = RandomBindingString()
sqrt: input => {
const ret = RandomBindingString()
const isNegative = RandomBindingString()
const isLowerThanTwo = RandomBindingString()
const next = RandomBindingString()
const nextEqualOrGreaterThan = RandomBindingString()
const isNextEqualOrGreaterThanRet = `(${nextEqualOrGreaterThan} * ${ret})`
const isNotNextEqualOrGreaterThanRet = `((not ${nextEqualOrGreaterThan}) * ${next})`
const lowerThanTwoPart = `(${isLowerThanTwo} * ${input})`
const notLowerThanTwoPart = `((not ${isLowerThanTwo}) * (${isNextEqualOrGreaterThanRet} + ${isNotNextEqualOrGreaterThanRet}))`
const negativePart = `(${isNegative} * -1)`
const notNegativePart = `((not ${isNegative}) * (${lowerThanTwoPart} + ${notLowerThanTwoPart}))`
return {
genBindings: [
{
source: `${n} / 2`,
target: g,
source: `(${input} < 2)`,
target: isLowerThanTwo,
},
{
source: `(${notAllow} * -1)`,
source: input,
target: ret,
},
{
source: `(${ret} + ${input} / ${ret}) / 2`,
target: next,
},
{
source: `(${next} = ${ret}) or (${next} > ${ret})`,
target: nextEqualOrGreaterThan,
},
{
source: `${negativePart} + ${notNegativePart}`,
target: ret,
},
],
@ -86,6 +109,10 @@ export const defaultFunctions = {
}
},
// str_length: str => {
// if (!/\#\w+/.test(str)) throw new Error("Invalid string")
// }
/**
* Return a translatable string
* @param key