訂判斷及所需輸入
本例格線每毫米八點,12.3 毫米對98.4 點,不能直接當整點設定。最接近取98,向上取99;兩種都不是在猜真正打印機預設。
| 情況 | 輸入 JSON |
|---|---|
| 原例測試 | {"tenthsMillimetre":123,"dotsPerMillimetre":8,"rounding":"nearest"} |
跑所附比較
程序先按十分之一毫米整數乘格線,再取點。幾何尺寸及所選整點數仍是不同記錄,供覆核取整影響。
| 路徑 | 輸出 JSON |
|---|---|
| 錯誤路徑 | {"dots":98.4,"executableInteger":false} |
| 所需路徑 | {"dots":98,"executableInteger":true} |
import assert from 'node:assert/strict';
const smallWhole=(x,max)=>Number.isSafeInteger(x)&&x>=0&&x<=max;
const run=function run(i,corrected=true){
if(!['nearest','ceil'].includes(i.rounding)||!smallWhole(i.tenthsMillimetre,1000000)||!smallWhole(i.dotsPerMillimetre,100)||i.dotsPerMillimetre===0)return {held:true};
const numerator=i.tenthsMillimetre*i.dotsPerMillimetre;
const dots=corrected?(i.rounding==='ceil'?Math.ceil(numerator/10):Math.floor((numerator+5)/10)):numerator/10;
return {dots,executableInteger:Number.isInteger(dots)};
};
const cases=[{"input":{"tenthsMillimetre":123,"dotsPerMillimetre":8,"rounding":"nearest"},"corrected":false,"expected":{"dots":98.4,"executableInteger":false}},{"input":{"tenthsMillimetre":123,"dotsPerMillimetre":8,"rounding":"nearest"},"corrected":true,"expected":{"dots":98,"executableInteger":true}},{"input":{"tenthsMillimetre":123,"dotsPerMillimetre":8,"rounding":"ceil"},"corrected":true,"expected":{"dots":99,"executableInteger":true}},{"input":{"tenthsMillimetre":125,"dotsPerMillimetre":8,"rounding":"nearest"},"corrected":true,"expected":{"dots":100,"executableInteger":true}},{"input":{"tenthsMillimetre":123,"dotsPerMillimetre":8,"rounding":null},"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);}
按邊界依據作判斷
剛好12.5 毫米就是100 點,沒有取整歧義;未選取整法便停。
- 取所附格線密度
- 先換算再取整
- 尺寸及所選點數一起留
取整法未訂就停,不能靜靜代選。
| 條件 | 輸入 JSON | 預期輸出 JSON |
|---|---|---|
| 明選向上取點 | {"tenthsMillimetre":123,"dotsPerMillimetre":8,"rounding":"ceil"} | {"dots":99,"executableInteger":true} |
| 換算剛好整點 | {"tenthsMillimetre":125,"dotsPerMillimetre":8,"rounding":"nearest"} | {"dots":100,"executableInteger":true} |
| 取整選擇未知 | {"tenthsMillimetre":123,"dotsPerMillimetre":8,"rounding":null} | {"held":true} |
原例最接近取整,應填哪個設定?
查看參考答案
98 整點。原12.3 毫米仍保留;98.4 只是換算結果,不是可執行整點數。