AdamWhiteHat / BigDecimal

An arbitrary-precision decimal (base 10) floating-point number class. Over 2.5 million downloads on NuGet!
MIT License
53 stars 15 forks source link

Does it support hexadecimal large number calculation #14

Closed qq1176914912 closed 1 year ago

qq1176914912 commented 1 year ago

If Inscribe

qq1176914912 commented 1 year ago

Regarding hexadecimal, the calculated BigDecimal can be converted to BigInteger, which is then converted to hexadecimal.

Protiguous commented 8 months ago

@qq1176914912, Were you asking if it is possible to use two strings in an operation like BigDecimal.Multiply(x,y)? Where x and y are both hexadecimal strings? And/or the resulting BigDecimal can also be shown as a hexadecimal string?

qq1176914912 commented 8 months ago

BigInteger

My idea at that time was to input the hexadecimal string directly to compute and output the hexadecimal result, and then I thought that I could convert to BigInteger and then convert to hexadecimal.

AdamWhiteHat commented 8 months ago

BigDecimal can be converted to BigInteger, which is then converted to hexadecimal.

Yeah, but then you are losing the decimal part.

No it doesn't support hexadecimal representation yet, but just like how Int32 and other numeric types support a "X" or "Xn" format specifier, I think we should too (Note: In "Xn", the n specifies the minimum number of digits desired, e.g."X2" or "X8"). See Microsoft - Hexadecimal format specifier for the specifics.

Note here that as far as format specifier strings go, all numeric types support the Binary ("B"), Currency ("C"") and Exponential (scientific) ("E") formats too. I think we already support the exponential specifier. Should we also support the Binary and Currency too? Currency is simple. Binary would be the same level of effort as the hexadecimal. Actually, we might as well support arbitrary base at that point. Write it once for arbitrary base. Funnily enough, this could all be done by easily by taking a dependency on my Polynomial library, but we wont do that. But the code there does show an example of how it could be done.

Implementation note: Microsoft says that the max value it supports for the precision identifier is Int32.MaxValue (or 999,999,999 for .NET 7, which is smaller for some reason). While it's probably not necessary to support beyond Int32.MaxValue, if our whole schtick is arbitrary precision, we might as well parse the precision identifier as a BigInteger and give the user enough rope to hang themselves, or give them the ammo they want for their foot-gun, or substitute in your own silly idiom here if needed.

This would be a useful feature. Not sure how much it would be used though.

Note to contributors who watch this library: I'm not asking anyone else to develop this, I don't mind. I've already written this type of code before.

QUESTION: If we support arbitrary base, how should that be specified? ToString("BaseN"), where N is a number?

qq1176914912 commented 8 months ago

BigDecimal can be converted to BigInteger, which is then converted to hexadecimal.

Yeah, but then you are losing the decimal part.

No it doesn't support hexadecimal representation yet, but just like how Int32 and other numeric types support a "X" or "Xn" format specifier, I think we should too (Note: In "Xn", the n specifies the minimum number of digits desired, e.g."X2" or "X8"). See Microsoft - Hexadecimal format specifier for the specifics.

Note here that as far as format specifier strings go, all numeric types support the Binary ("B"), Currency ("C"") and Exponential (scientific) ("E") formats too. I think we already support the exponential specifier. Should we also support the Binary and Currency too? Currency is simple. Binary would be the same level of effort as the hexadecimal. Actually, we might as well support arbitrary base at that point. Write it once for arbitrary base. Funnily enough, this could all be done by easily by taking a dependency on my Polynomial library, but we wont do that. But the code there does show an example of how it could be done.

Implementation note: Microsoft says that the max value it supports for the precision identifier is Int32.MaxValue (or 999,999,999 for .NET 7, which is smaller for some reason). While it's probably not necessary to support beyond Int32.MaxValue, if our whole schtick is arbitrary precision, we might as well parse the precision identifier as a BigInteger and give the user enough rope to hang themselves, or give them the ammo they want for their foot-gun, or substitute in your own silly idiom here if needed.

This would be a useful feature. Not sure how much it would be used though.

Note to contributors who watch this library: I'm not asking anyone else to develop this, I don't mind. I've already written this type of code before.

QUESTION: If we support arbitrary base, how should that be specified? ToString("BaseN"), where N is a number?

Thank you for taking the time to reply. I think your idea is very good and can be easily understood by using "ToString("BaseN").