Custom ValueType to Accept 128-bits

  • Thread starter Thread starter Shawn B.
  • Start date Start date
S

Shawn B.

Greetings,

I know I can create class that "implicit"ly accepts an 8-bit to 64-bit
value. Without using double, or single, I would like to create my own
"unsigned" 128-bit valuetype (to be used with my Binary objects I recently
created http://www.visualassembler.com/binary) but I don't know how I can do
so. How can I make it accept the number 2309823982039203948203948? I don't
want to have to pass the numbers as a string, I just want to assign it a
number and expect that it'll work.

I know I can use 64-bit because I just implicit with a ulong as a parameter
type, but I don't know how to go about creating my own 128-bit type...

Any ideas?

Thanks,
Shawn
 
Hi Shawn,

I don't think you'll ever get a statement like the following to compile:
Binary128 b = 2309823982039203948203948;
The compiler will have no idea what you're talking about. :)
Perhaps a Binary128 could consist of two Binary64 with some logic to hide that,
but I don't think that you'll be able to initialize it with a value bigger than 64bits, unless it's initialized with another
Binary128.
 
You can do this with two 64 bit values, but you'll have to write a lot of
code. Mathematical operations will be quite difficult, as there is no
overflow bit (and a checked overflow condition throws an exception, which
would kill your performance on simple mathematical operations).

I haven't seen a good arbitrary-precision integer library for C#, but maybe
you should be looking for something along those lines, perhaps in the C/C++
world.
 
I was looking in the Mono source code and they have a
Corlib.Mono.Math.BigInteger object that takes 160 bits of memory. It is a
rather interesting piece of work. There is a possibility my answer lies in
in examining that source code.


Thanks,
Shawn
 
Hi,
If you don't want to use strings for initializing your "big" value type the
compiler has to support such a big litterals.
I'm not sure MS c# compiler can handle those litterals. Maybe in the MONO
project they have modified their compiler.

B\rgds
100
 
Back
Top