Discuss Scratch

ipaddude
Scratcher
100+ posts

Represent signed decimal floating-point numbers using unsigned decimal integers

So I'm making a weakly typed language on scratch inspired by machine language, but using some native features of scratch, so decimal is used instead of hex. However, signed numbers and floating-point numbers are not supported because of the nature of the language (inspired by machine language, can be used interchangeably with its supported data) and for easy data storage and interpretation.

However, I would like to be able to represent signed decimal floating-point numbers using unsigned decimal integers, so that I can perform mathematical operations (including multiplication, division, and preferably trig) on any number as any representation of any numeric type. (converted appropriately) Is there a way to represent signed numbers and signed floating-point numbers using unsigned decimal integers? I know that there are methods of representing signed binary numbers such as 2's complement; are there equivalents for decimal?


tl;dr I want to represent signed decimal and signed floating-point decimals using unsigned decimals so that when converted back they are correctly modified by operations done on the unsigned decimals (e.g. excess-K won't work because it doesnt work with multiplication)
awesome-llama
Scratcher
1000+ posts

Represent signed decimal floating-point numbers using unsigned decimal integers

Yes there is, but it might be a bit weird to think about.

Instead of treating the decimal numbers as numbers, you have to treat them as data. I think you'll have to come up with your own storage methods though.

Single-precision floating point number example:

Here's what it looks like in binary.



Your decimal version could follow the same format, except you have more numbers to work with (base 2 vs base 10).
You could keep the sign as the same single bit, but have the decimal version just not use any of the other numbers. For the exponent and fraction (which in binary are just binary numbers), you can also do the same thing - just store the number in base 10.
Overall, you then just have one digit for the sign, then two decimal numbers after that acting as the two other parts.

Powered by DjangoBB