Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define custom float-point format (type) in C++?

How can I define my own float-point format (type) with specific precision and certain bitness of exponent and significand? For example, 128-bit float-point number with 20-bit exponent and 107-bit significand (not standart 15/112-bit), or 256-bit one with 19/236-bit exponent/significand.

like image 246
Eugene Avatar asked Jan 10 '15 23:01

Eugene


People also ask

How do you represent a floating-point in C?

Any number that has a decimal point in it will be interpreted by the compiler as a floating-point number. Note that you have to put at least one digit after the decimal point: 2.0, 3.75, -12.6112. You can specific a floating point number in scientific notation using e for the exponent: 6.022e23.

How do you set a precision float?

To set the precision in a floating-point, simply provide the number of significant figures (say n) required to the setprecision() function as an argument. The function will format the original value to the same number of significant figures (n in this case).

What is a floating-point type?

The floating-point data type is a family of data types that act alike and differ only in the size of their domains (the allowable values). The floating-point family of data types represents number values with fractional parts. They are technically stored as two integer values: a mantissa and an exponent.

How do you increase the precision of a floating point number?

Store the value in a higher-precision variable. E.g., instead of float step , use double step . In this case the value you've calculated won't be rounded once more, so precision will be higher.


1 Answers

There are 2 ways to do this. You can create your own class where you have a member for the exponent and a member for the mantissa, and you can write code for the operators you need, and then implement all of the functions you'd need that normally exist in the standard math library. (Things like atan(), sin(), exp() and pow().)

Or you can find an existing arbitrary precision library and use it instead. While implementing it yourself would be interesting and fun, it is likely to have a lot of errors in it and to be an extremely large amount of work, unless your use-case is extremely constrained.

Wikipedia has a list of arbitrary precision math libraries that you can look into for yourself.

like image 164
user1118321 Avatar answered Oct 01 '22 17:10

user1118321