Recent

Author Topic: Making 'powers' into human readable decimal representations  (Read 18750 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Making 'powers' into human readable decimal representations
« on: March 16, 2011, 11:21:01 pm »
Hi

I want to make a quick GUI with buttons representing a 128 bit value, a 160 bit, 256 bit and 512 bit value that, when clicked, give a decimal representation of just how enormous the value is.

For example, 128-bit produces a value of 2^128 = 3.40282367 × 1038, or, 340 billion billion billion billion billion, so when the 128-bit button is pressed, I'd want that ...billion billion... to be printed to screen. 160 bit produces 1.46150164 × 1048, or, well I don't know how many trillions that is. And so on. 

Would I be best using 'power' (http://www.freepascal.org/docs-html/rtl/math/power.html) and intToPower (http://www.freepascal.org/docs-html/rtl/math/intpower.html) for this task?

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Making 'powers' into human readable decimal representations
« Reply #1 on: March 17, 2011, 12:18:41 am »
Yes, you will have to use power() function, because the biggest integer format is QWord (64-bit) and it is not enough. You will need Double (64 bits) or Extended (80 bits), which can calculate 2^512.

Precission: power(2, 512) will return 1.34078079299426E154, i.e.  1.34078079299426*10^154  - so it is not absolutely exact (you probably know).

Problems with display:

I tried floattostrF with parameters ffFixed, ffNumber, ffGeneral or ffCurrency and none of them displayed so many zeros.  :(

Maybe someone know better way how to do it.
If not then you have chance to do it manually:
1, Take result string: 1.34078079299426E154
2, Remove decimal point and part with E154.
3, Add 154 zeros to string.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

mas steindorff

  • Hero Member
  • *****
  • Posts: 571
Re: Making 'powers' into human readable decimal representations
« Reply #2 on: March 17, 2011, 12:19:22 am »
I've not used the code your referenced above but I doubt if it will do what you what.  
Instead, you may need to do your own decoder.  After you put the number into a var (extended type), you can divide it by 1 billion and test the results.  If it is more than 1 then print a "billion" (or just count it) and divide and test the results again (each time in a loop).  when the results are less than 1, then go back to your "start of loop" value and print it in front of all of the "billions".  You can do this trick with other bases (trillions, thousands, hundreds...) as well but start with the biggest and work your way down.
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Making 'powers' into human readable decimal representations
« Reply #3 on: March 17, 2011, 09:21:37 pm »
Blaazan

Quote
If not then you have chance to do it manually:
1, Take result string: 1.34078079299426E154
2, Remove decimal point and part with E154.
3, Add 154 zeros to string.

I know I am showing my lack of understanding here, but there is a big difference between 1.XXX... and 1XXX...

If I remove the decimal, then before adding the 154 zeroes, I still have a figure of 134,078,079,299,426

If I then add the zeroes, you're looking at

134,078,079,299,426,000,000,000,000,000,000,000,000,000,000,000,000, and so on....

Shouldn't it be that I take just the starting portion of the decimal so that it becomes 134,000,000,000,000,000 and so on. I am confused! :-(  

2^128 = 3.40282367 × 1038 = 38 zeroes (340,000,000,000,000, 38 times)
2^160 = 1.46150164 × 1048 = 48 zeroes (146,000,000,000,000,000, 48 times)
2^256 = 1.15792089 × 1077 = 77 zeroes (115,000,000,000,000,000,000, 77 times)
2^512 = 1.34078079 × 10154 = 154 zeroes (134,000,000,000,000,000,000,000, 154 times)
« Last Edit: March 17, 2011, 09:36:17 pm by tedsmith »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Making 'powers' into human readable decimal representations
« Reply #4 on: March 17, 2011, 11:24:56 pm »
I mistaken. Don't add 154 zeros, but only ~140 zeros if you remove decimal point.

1.34078079299426E154 -> 134078079299426 E140

I would do this:

find position of E

Code: [Select]
var str, finalstr: string;
      i, pos: word;
begin
  str:=floattostr(power(2, 512));
  for i:=1 to length(str) do
    if subs[i]='E' then pos:=i;
                             

Now you have position of E.
Using procedures / functions in strutils you can do the rest ( LeftStr, MidStr, RightStr).
Code: [Select]
finalstr:=LeftStr(str, 1)+MidStr(str, 3, pos-1);
for i:=1 to strtoint(RightStr(Str, pos+1)) do
  finalstr:=finalstr+'0';

Not tested  :)

if you want also  000,000,000 (thousand separators) then you can do it similar way using MidStr(finalstr, ..., 3) in loop from right to left. Number of loops will be
Code: [Select]
length(finalstr) div 3

Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2680
Re: Making 'powers' into human readable decimal representations
« Reply #5 on: March 18, 2011, 10:29:54 am »
I don't know if you want all those zeroes.
First you really need to count them to know what value is presented,
second and more important, with adding those zeroes you imply a precision in the given value which is not true. So you are misleading your users.
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Making 'powers' into human readable decimal representations
« Reply #6 on: March 21, 2011, 10:30:34 pm »
Marc

It's just a casual way of demonstrating to people who visit our office how big the numbers are. Presenting the result of 'power' to them will mean nothing to many of them. But a big number with hundreds of zeroes following it will. So it doesn't need to be spot on exact.

Blaazen

What is the subs function? My Lazarus install is not detecting it and I can't find it in the docs? I assume it "substitute" or something?




DirkS

  • Sr. Member
  • ****
  • Posts: 251
Re: Making 'powers' into human readable decimal representations
« Reply #7 on: March 21, 2011, 10:41:26 pm »
Quote
What is the subs function?
I think that should be str[ i ] instead of subs[ i ]

Gr.
Dirk.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Making 'powers' into human readable decimal representations
« Reply #8 on: March 21, 2011, 10:45:25 pm »
Quote
I think that should be str[ i ] instead of subs[ i ]

Gr.
Dirk.

Yes, thank  you, Dirk.

(subs was subString, I forgot to repair it)
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

bflm

  • Jr. Member
  • **
  • Posts: 54
    • Free Pascal Random Bits
Re: Making 'powers' into human readable decimal representations
« Reply #9 on: March 21, 2011, 11:24:40 pm »
Hi

I want to make a quick GUI with buttons representing a 128 bit value, a 160 bit, 256 bit and 512 bit value that, when clicked, give a decimal representation of just how enormous the value is.

For example, 128-bit produces a value of 2^128 = 3.40282367 × 1038, or, 340 billion billion billion billion billion, so when the 128-bit button is pressed, I'd want that ...billion billion... to be printed to screen. 160 bit produces 1.46150164 × 1048, or, well I don't know how many trillions that is. And so on. 

Would I be best using 'power' (http://www.freepascal.org/docs-html/rtl/math/power.html) and intToPower (http://www.freepascal.org/docs-html/rtl/math/intpower.html) for this task?

It's probably only a handful lines of code using the standard FPC GMP bindings unit. It has both the big int math operations (powint included) as it has the conversions to/from string. Optionally using the extended bindings and overloaded operators makes all of this even easier (no manual memory management required and the string conversion comes for free on assignment/argument passing).

http://wiki.freepascal.org/gmp
« Last Edit: March 21, 2011, 11:26:13 pm by bflm »

TurboRascal

  • Hero Member
  • *****
  • Posts: 672
  • "Good sysadmin. Bad programmer."™
Re: Making 'powers' into human readable decimal representations
« Reply #10 on: March 22, 2011, 09:29:14 am »
I believe Marc is right regarding implied precision. It may still be more correct to write "billions", "trillions" etc. which doesn't imply any further precision... A bit more work to decode than zeros, but far more true.
Regards, ArNy the Turbo Rascal
-
"The secret is to give them what they need, not what they want." - Scotty, STTNG:Relics

Gizmo

  • Hero Member
  • *****
  • Posts: 831
OK, all the finite accuracies aside (this app is to help explain the enormity of the numbers to normal, non IT and non mathmatical people, as a generalisation), I need some super mathmatical help.

I have nearly finished my program but I want to check that I've done it correctly. Myself and FabienWang have talked it to death in the forum and we agreed to ask here for some mathmatical super intelligence

OK, so:

A) power(2^128) = 3.40282366920938E38
B) power(2^160) = 1.4615016373309E48

I understand A to mean "340 followed by 38 zeroes". There are 9 zeroes to a billion. 38 / 9 = 4.22. So give or take a billion, I take that to be 340, billion, billion, billion, billion (4 billion groups) and so does the forensics company NTI in their page here (http://www.forensics-intl.com/art12.html - see half way down).

So my program calculates all those zeroes and outputs the following info :

http://img268.imageshack.us/i/screenshotform1.png/

On that same basis, I understand B to mean "146 followed by 48 zeroes". 48 / 9 = 5.3. So give or take a billion, I take that to be 146, billion, billion, billion, billion, billion (5 billion groups). I have no one to reference on that though.

http://img863.imageshack.us/i/screenshotform11.png/

So, my questions are two fold :

Is A) correct? Or is it 3.4 billion billion billion billion (instead of 340)?
Are my works for B) correct? Or is it 14 billion, billion, billion, billion, billion, 146 billion, billion, billion, billion, billion or 1460 billion, billion, billion, billion, billion or 1.4 trillion, trillion, trillion, trillion (48 / 12 (for trillion) = 4)?

I am totally confused and maths is not my strong point. I need some help with this and assume you are talking to an idiot who is aged about 12 when you explain! I won't be offended and I need to try and understand this!

Thanks

« Last Edit: March 27, 2011, 10:55:21 pm by tedsmith »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Making 'powers' into human readable decimal representations
« Reply #12 on: March 27, 2011, 11:10:06 pm »
Incorrect !

A) 3.40E38 is 340 followed by 36 zeros
B) 1.46E48 is 146 followed by 46 zeros

but on your screenshot it is correct:

340 bilion, bilion, bilion, bilion
(if bilion has 9 zeros then 4 * 9 = 36)

Note than billion has nine zeros in US but twelve zeros in Europe  :D
« Last Edit: March 27, 2011, 11:17:07 pm by Blaazen »
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Making 'powers' into human readable decimal representations
« Reply #13 on: March 28, 2011, 12:35:24 am »
Thanks mate.

I have also discovered the amazing http://www.wolframalpha.com/ which helps. Just enter the power value and it does some nifty workings out and has helped me confirm that 2^160 is 1.4 trillion trillion trillion trillion and 1^128 is 340 billion billion billion billion.

fabienwang

  • Sr. Member
  • ****
  • Posts: 449
  • Lazarus is the best
    • My blog
Re: Making 'powers' into human readable decimal representations
« Reply #14 on: March 28, 2011, 12:59:46 am »
oh i was right about the 340 hehe
I'm using Arch Linux.
Known for: CPickSniff, OpenGrabby
Contributed to: LazPaint

 

TinyPortal © 2005-2018