stillwater-sc / universal

Large collection of number systems providing custom arithmetic for mixed-precision algorithm development and optimization for AI, Machine Learning, Computer Vision, Signal Processing, CAE, EDA, control, optimization, estimation, and approximation.
MIT License
412 stars 59 forks source link

2's complement for negative posit numbers #445

Closed RookieT0T closed 1 month ago

RookieT0T commented 1 month ago

posit<32, 2> temp = -2.5; std::cout << to_binary(temp) << std::endl;

In this case, to_binary() function returns "0b1.10.01.010000000000000000000000000", I am not sure if this is valid. It seems incorrect because the function first encodes the positive number 2.5 and assigns 1 to the most significant bit to make it negative. I am skeptical about this process because 2's complement needs to be used for encoding or decoding negative posit numbers. I believe "1.01.10.110000000000000000000000000" is the encoded binary for -2.5.

I am an undergraduate right now and new to posit. This is my naive thought. If it is not a bug, some explanation is much appreciated.

RookieT0T commented 1 month ago

Is this just for better readability? I extracted the bits stored in memory and the bits were exactly "1.01.10.110000000000000000000000000"

Ravenwater commented 1 month ago

@RookieT0T does this help?

https://github.com/stillwater-sc/universal/blob/main/docs/posit-refinement-viz.md

If this documentation needs more articulation, let me know.

The posit decode was designed before the final spec was rectified, In the early days, we did the two's complement to get to the regime/exponent/fraction bits.

Ravenwater commented 1 month ago

@RookieT0T

Here is the output of the posit manipulator test: image

The to_binary() and color_print() methods are showing the posit components. The get() method gets you the raw bits.

RookieT0T commented 1 month ago

Oh, I see. Given the same value, what is returned by to_binary() is different from the raw bits stored in the memory. Thanks for your clarification.

RookieT0T commented 1 month ago

I believe arithmetic operations of this library are based on the raw bits instead of things printed out from to_binary(), right?

Ravenwater commented 1 month ago

@RookieT0T, correct. The posit arithmetic type uses posit encodings for storage and implements value arithmetic, so it can be used as a plug-in replacement for native float and double.

Ravenwater commented 1 month ago

@RookieT0T The regression suite for posits will give you a sense of what you can do with the posit<> type:

image

There is also a collection of application skeletons that you can look at. When we do mixed precision studies for computational science or engineering, we typically take a posit, a cfloat, a fixpnt, and potentially an lns to track precision, accuracy, and performance.

RookieT0T commented 1 month ago

thanks for pointing them out