Polish UI

This commit is contained in:
BorysLevytskyi
2023-05-17 14:38:32 +02:00
parent f04bd3268f
commit dd2a7b6472
6 changed files with 32 additions and 8 deletions

View File

@@ -48,4 +48,10 @@ it('converts to largest size', () => {
expect(n8.num()).toBe(-1);
expect(n16.num()).toBe(-1);
expect(n32.num()).toBe(-1);
});
});
it('abs doesnt change type size', () => {
const result = Integer.byte(-127).abs();
expect(result.num()).toBe(127);
expect(result.maxBitSize).toBe(8);
})

View File

@@ -47,6 +47,12 @@ export class Integer {
return new Integer(value, 8);
}
abs() : Integer {
return new Integer(
this.value >= 0 ?
this.value : -this.value, this.maxBitSize, this.signed);
}
isTheSame (other : Integer) : boolean {
return this.value == other.value && this.signed == other.signed && this.maxBitSize == other.maxBitSize;
}

View File

@@ -186,6 +186,18 @@ describe("calc misc", () => {
const actual = calc.not(asInteger("8920390230576132")).toString();
expect(actual).toBe("-8920390230576133");
});
it('numberOfBitsDisplayed appends sign bit', () => {
const byte = Integer.byte(-127);
const int = Integer.int(-127);
expect(calc.numberOfBitsDisplayed(int)).toBe(7);
expect(calc.numberOfBitsDisplayed(int.abs())).toBe(7);
// If there is only sign bit left, might as well show it
expect(calc.numberOfBitsDisplayed(byte)).toBe(8);
expect(calc.numberOfBitsDisplayed(byte.abs())).toBe(8);
})
});
describe("calc.engine.", () => {

View File

@@ -2,12 +2,11 @@ import { Integer, JsNumber, asInteger } from "./Integer";
import { asIntN, logLines } from "./utils";
export default {
abs (num : Integer) : Integer {
return asInteger(num.value >= 0 ? num.value : -num.value);
},
numberOfBitsDisplayed: function (num: Integer | JsNumber) : number {
return this.toBinaryString(asInteger(num)).length;
const n = asInteger(num);
const len = this.toBinaryString(n).length;
return (len+1) == n.maxBitSize ? n.maxBitSize : len; // Include sign bit if it is all that left
},
flipBit: function(num: Integer | JsNumber, bitIndex: number): Integer {
@@ -43,7 +42,7 @@ export default {
toBinaryString(num: Integer) : string {
const bitSize = num.maxBitSize;
const bin = this.abs(num).value.toString(2);
const bin = num.abs().value.toString(2);
if(bin.length > bitSize!)
throw new Error(`Binary represenation '${bin}' is bigger than the given bit size ${bitSize}`)

View File

@@ -1,5 +1,6 @@
import React from 'react';
import './BinaryString.css';
import loglevel from 'loglevel';
export type BinaryStringViewProps = {
allowFlipBits?: boolean;

View File

@@ -10,7 +10,7 @@ const formatter = {
switch(base) {
case 16:
var hexVal = calc.abs(num).value.toString(16);
var hexVal = num.abs().value.toString(16);
return num.value >= 0 ? '0x' + hexVal : '-0x' + hexVal;
case 2:
const bin = calc.toBinaryString(num);