if you read the original question:
I interpret that to mean he is after how many bytes does the number occupy, which is what I provided.
Printable View
I think the OP is just not worded in a way that would be clear to everyone. It seems that what he is looking for is the number of digits in a numeric variable and unfortunately is referring to them as bytes causing confusion.
As I mentioned, convert to string and check the length of that string and he will get the result that he is apparently looking for here.
Precisely - that's the the problem here: the question is not well formulated, most likely because, at the time it was asked, the OP didn't have a precise understanding of the relationship between the types and their values.
So, people came up with different interpretations and offered different solutions.
You also have to take note that the OP didn't say in what way exactly he would use this, so we can't draw any conclusions from that.
For example, MadHatter's method could be perfectly valid if the OP needed to do some sort of quantization.
Anyway, my point is, only the OP can tell us what is it that he's actually after here, provided that he himself has a good understanding of the topic.
Though I know i'm beating a dead dog here, I think i'll still respond one more time ;)
That is very ambiguous and as written makes absolutely no sense. There are three ways this could be interpreted. I left rough estimates as to the memory costs at the end of each line.Quote:
I'm not after the size of the int, or the double, or the long.
I'm after the size of the variable that is 500 digits long.
How long is that in bytes? I hope I'm being clear. My apologies for any confusion.
1) If i call "ToString()" on the number, i will end up with a string of 500 characters which will be 500 x sizeof (char) bytes in size. (1000 bytes)
2) This could also be validly represented by 500 x sizeof (byte) which is smaller again by 50%. (500 bytes)
3) It could also be represented by a handful of bytes. At a completely random guess, 10 bytes may be enough if you used standard binary notation. Someone else can do the math and work it out exactly. It'll be a lot less than 500 though. (about 10 bytes)
So that's three ways to represent your number, each of which have different memory requirements. That's why this question is so ambiguous. Your question implies that you want either answer 1 or 2 but it's impossible to tell so rather than argue uselessly about it I'll leave it at this ;)
And, in addition to what Mutant_Fruit said, if you assume that IntX dynamically adapts memory-wise, you can also interpret the question as: "What is the amount of memory used by an IntX instance to represent a certain number?".
But, (1) this is totally dependent on the actual implementation, and (2): what makes you think that the answer to this question would always be the same, even when calling the hypothetical "GetSize()" method once, and then calling it again only few lines later?
Consider this: two IntX objects could contain the same value, but if the "usage-history" had been different, the memory requirements may have been different also, so the two instances would use different amount of bytes to represent the same number. (Again - depending on the implementation.)
Furthermore, if you interpret this as the minimum amount of bytes required to represent an n-digit long number, then you can get the answer mathematically - you don't need to bother with the IntX type or it's instances and values at all.
Again, I think that the implementation details are open-source, but otherwise you're right - this should be the responsibility of the class, in which case the documentation would define what exactly the number returned by my hypothetical "GetSize()" method means.
"the number of bytes an X digit number is" sounds like the way oracle stores numbers. its' a feasible question, but due to the way most of us think of number systems, its quite different.
My apologies for the slow reply but I wanted to close the loop on this in case others in the future have a similar question. Yes, my question was worded in a manner that instantly muddied the water and caused a lot of confusion.
Zaccheus had it right when he said:
The answer is that the IntX class has a method called "GetInternalState()" which provides the answer I was looking for.Quote:
He wants to know how many bytes an instance of IntX uses for a 500 digit number.
Tada!
(@OP: You can mark the thread as resolved by clicking the "Thread Tools" menu at the top and selecting "Mark Thread Resolved")