訂判斷及所需輸入
本例先把分數取整,再查結果絕對值是否至少三。半數取偶把5/2 成二,條件否;半數遠離零成三,條件是。程序用整數分子、分母及 BigInt 商餘數,不是二進位小數相等問題。
| 情況 | 輸入 JSON |
|---|---|
| 原例測試 | {"numerator":5,"denominator":2,"policy":"ties-even","threshold":3} |
跑所附比較
兩種算法由本例明訂。分子可正可負,但須為安全整數;分母須為正安全整數,門檻則是非負安全整數。整數零以0保留,不推真正規則引擎預設。
| 路徑 | 輸出 JSON |
|---|---|
| 錯誤路徑 | {"rounded":3,"predicate":true} |
| 所需路徑 | {"rounded":2,"predicate":false} |
import assert from 'node:assert/strict';
const run=function run(i,corrected=true){
if(!['ties-even','ties-away'].includes(i.policy)||!Number.isSafeInteger(i.numerator)||!Number.isSafeInteger(i.denominator)||i.denominator<1||!Number.isSafeInteger(i.threshold)||i.threshold<0)return {held:true};
const signed=BigInt(i.numerator),n=signed<0n?-signed:signed,d=BigInt(i.denominator);let q=n/d;const r=n%d;
const away=!corrected||i.policy==='ties-away';if(r*2n>d||(r*2n===d&&(away||q%2n===1n)))q++;
const rounded=Number(signed<0n?-q:q);return {rounded,predicate:Math.abs(rounded)>=i.threshold};
};
const cases=[{"input":{"numerator":5,"denominator":2,"policy":"ties-even","threshold":3},"corrected":false,"expected":{"rounded":3,"predicate":true}},{"input":{"numerator":5,"denominator":2,"policy":"ties-even","threshold":3},"corrected":true,"expected":{"rounded":2,"predicate":false}},{"input":{"numerator":-5,"denominator":2,"policy":"ties-even","threshold":3},"corrected":true,"expected":{"rounded":-2,"predicate":false}},{"input":{"numerator":-5,"denominator":2,"policy":"ties-away","threshold":3},"corrected":true,"expected":{"rounded":-3,"predicate":true}},{"input":{"numerator":51,"denominator":20,"policy":"ties-even","threshold":3},"corrected":true,"expected":{"rounded":3,"predicate":true}},{"input":{"numerator":-49,"denominator":20,"policy":"ties-even","threshold":3},"corrected":true,"expected":{"rounded":-2,"predicate":false}},{"input":{"numerator":5,"denominator":2,"policy":null,"threshold":3},"corrected":true,"expected":{"held":true}},{"input":{"numerator":5,"denominator":0,"policy":"ties-even","threshold":3},"corrected":true,"expected":{"held":true}}];
for(const test of cases){const actual=await run(test.input,test.corrected);assert.deepEqual(actual,test.expected);console.log(actual);}
按邊界依據作判斷
負−5/2 分出−2、−3;附近51/20、−49/20 試非半數。算法未知或分母零便停。
- 訂清半數取整法
- 跑正負分界
- 對最後條件判斷
引擎未訂算法,不能自行估預設。
| 條件 | 輸入 JSON | 預期輸出 JSON |
|---|---|---|
| 負數剛好半數 | {"numerator":-5,"denominator":2,"policy":"ties-even","threshold":3} | {"rounded":-2,"predicate":false} |
| 負半數明訂遠離零 | {"numerator":-5,"denominator":2,"policy":"ties-away","threshold":3} | {"rounded":-3,"predicate":true} |
| 正數剛超半數 | {"numerator":51,"denominator":20,"policy":"ties-even","threshold":3} | {"rounded":3,"predicate":true} |
| 負數幅度未到半數 | {"numerator":-49,"denominator":20,"policy":"ties-even","threshold":3} | {"rounded":-2,"predicate":false} |
| 取整算法未知 | {"numerator":5,"denominator":2,"policy":null,"threshold":3} | {"held":true} |
| 分母零不在規則內 | {"numerator":5,"denominator":0,"policy":"ties-even","threshold":3} | {"held":true} |
算式文字相同,原例條件就可直接搬嗎?
查看參考答案
不是。半數規則令二或三不同,條件也變否或是。使用前按已訂算法把正負兩邊重試。