Positional number systems

Asked by johan l vgren

In my project (C ++, GUI in C #), it was necessary to work with different number systems (2,6,10,16 and not only). Moreover, perform mathematical operations on them (addition, subtraction, multiplication, division). We must work with both natural and real numbers. And ideally, to get the output of the whole and fractional parts in the form of, for example, the line - to show to users.

It is necessary to call a function, for example, addition with two arguments: 1 - a natural number in the decimal system (2300), 2 - a real number in the ternary system ("2120.102")

Are there any libraries (in C ++ or C #) that could provide this functionality?

If there are none, would it not be better to implement it in some functional language (although it probably already exists) and connect it with C ++ or C #?

And you know exactly what you need? Why store and perform calculations in different number systems? Still, all the same in the binary turns. Or is there some kind of profit from the given number systems? If this is an educational work, I advise you to write with your hands. - linda schnetzer
I will operate, naturally, with one number and I am not going to store numbers in different systems (it just makes no sense). And the question itself was whether there are libraries with already implemented functionality for mate. operations. Well, that is, as I see the call, for example, the addition function in the found library: the first argument is a string with a real n1-ary number ("222010.222" is a triple), the second argument is a string with a real n2-ary number ("424010.2401" - 5-ric), the third one is the type of the resulting number system - m-ary number system (hex-ric), and at the output I want to get the summing line in the m-ary system format ("FF3.44D"). - suzy page
Ie you expect to see something like this:
string Add_3base_to_5_base_ret_16_base (string base_3, string base_5);
? This is how many functions should be declared in such a library? Yes, and why?

If so, then wait in vain, because everything is solved in another way:
to_base (from_base (base3, 3) + from_base (base5, 5), 16);

Conversion options to find no problem. And is there really a problem - to write two conversion functions, and, if necessary, one class with 4 overloaded operators and the necessary wrappers for operations on the mat? - tracey holden
Yes, I meant exactly the format that you wrote - just hurry with the answer ...
About the fact that the problem - no, not a problem. Probably the question of a jigger is not worth it ... - ashley espey
Why do you even need to operate with a number in a strange number system? Store in one, in binary as usual, lead to it, read it in it and then output it as it should. - tania hutley
Above, I have already answered that this is exactly what I intended to do, the whole essence of the question comes down to the question, is there a ready-made code (function) for converting, conducting the mat. operations with real numbers.
And yes, the conversion algorithms are known to me. - shrabonti
Once again. There is a conversion. Operations, why? Operations are performed in the usual way with the usual binary representation in hardware, the result is displayed as we want. For integers there is link For real ones, I don’t know. - seng chuen
Hmm ... yes, you are right, the main thing is conversion, but you don’t need operations ... Yes, I’ve been too smart about something ... - neni


You mix logic with display. (And this is one of the greatest sins in programming)
There are no numbers in the decimal system or in the ternary system - numbers are numbers. There is a decimal number recording system. (Ie talking about the display).
If you correctly divide, then you will have - converters from string to numeric and back, and the usual mathematical functions.

For example: create a Number class, add functions to it:
Number (& lt; recorded_string & gt ;, & lt; bit & gt;)
__toString (& lt; bit depth & gt;)
And overloaded addition, subtraction, division operators.
I simply incompletely formulated the question. My logic and display are in no way confused. Above, I added a question. I understand that all intermediate operations will be of the form: from a string from the X-nar system to the 10th (well, essentially binary, but we will not go deeper), then performing the operations, then output in string format to Y-ary. - leyla
heather rowland
a couple of years ago had a good time with a similar thing (I wrote a calculator myself), but I had to write classes myself: I didn’t google anything like that.

True, I kept everything in the form of arrays (and the order, and the mantissa, and the foundation of the SS) and performed the actions on them programmatically. It was clearly very slow. But he illustrated work with different bases of the number system.
Is there something like this in essence my goal :) - valentino
is there on a githaba? It would be interesting to see - rachel barkley
How to publish a translation? :: How to sort letters in GMail by the amount of attachments? :: A game about sand in linux? :: Reverse proxy: Squid VS nginx? :: Merchants for SaaS?
Leave Repply for Positional number systems
Useful Links