I am new 2 FreeBasic.

New to FreeBASIC? Post your questions here.
2
Posts: 75
Joined: Mar 28, 2006 21:01

I am new 2 FreeBasic.

Postby 2 » Mar 28, 2006 21:15

I used QBasic,SmallBasic,JustBasic,C,and JavaScript,but FreeBasic
is so cool. Somebody was telling me that I can use big_int
2 get really large numbers,but I reinstalled with all the available libraries
and I still don't know what 2 do! I tried this.

Code: Select all


print 2^256
sleep



But it only gives

1.15792089237316e+077

JustBasic has it built in and can numbers higher than 2^65536.
You can probably guess who I am.I've only infected half the internet.

http://www.angelfire.com/crazy/chandlerklebs
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Mar 28, 2006 21:46

Calc give me this:

1.1579208923731619542357098500869e+77 (move the decimal 77 places to the right now, and we have our number)

Scientific notation 2. :D
counting_pine
Site Admin
Posts: 6242
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Postby counting_pine » Mar 28, 2006 21:53

You could try the big_int library. I don't know much about it, but an example is included with FreeBasic. Have a look in the examples\big_int folder. I hope it is of some use to you.
v1ctor
Site Admin
Posts: 3804
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Postby v1ctor » Mar 28, 2006 22:09

The reason is quite simple, speed:

Code: Select all

start = time$("ms")
for i = 0 to 100000-1
 j = j + i
next
print time$("ms")-start


'' result: 1625 ms

Code: Select all

start# = timer
for i = 0 to 100000000-1
 j = j + i
next
print int((timer-start#) * 1000)


'' result: 495 ms

Notice that the latter loop iterates 3 orders of magnitude more in FreeBASIC (1000 times more), yet it takes less than 1/3 to finish. FB is 3000 faster than FirstBasic in that example.

So a specialized library has to be used, if you know C big_int won't be too hard to use, but far from been simple as if it was supported implicitly.
2
Posts: 75
Joined: Mar 28, 2006 21:01

I just can't figure it out.

Postby 2 » Mar 28, 2006 22:27

The purpose of this is 2 achieve really big numbers.
I just wish it was as simple as doing something with it that automatically
made the integers go that high.I know somebody will eventually help!
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Mar 28, 2006 22:41

um, hello, look up the term "scientific notation" and call us back, thanks.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Mar 28, 2006 22:46

cha0s wrote:um, hello, look up the term "scientific notation" and call us back, thanks.
^_^;; Now now, what did we talk about? Don't drive away developers!!!

2, that number IS that high! It's just in scientific notation when printed!! :D
tunginobi
Posts: 655
Joined: Jan 10, 2006 0:44
Contact:

Postby tunginobi » Mar 28, 2006 23:37

anonymous1337 wrote:1.1579208923731619542357098500869e+77

And that's equal to:

1.1579208923731619542357098500869 * 10 ^ 77

in scientific notation.
Fragmeister
Posts: 545
Joined: Nov 08, 2005 14:36

Postby Fragmeister » Mar 29, 2006 0:26

or 115,792,089,237,316,195,423,570,985,008,690,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000. (approximately.)
v1ctor
Site Admin
Posts: 3804
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Postby v1ctor » Mar 29, 2006 0:58

2 ^ 65536 using big_int (it takes around 2 secs to convert the result to a string):

Code: Select all

option explicit

#include once "big_int/big_int_full.bi"

#ifndef NULL
#define NULL 0
#endif

sub print_num (byval num as big_int ptr)
    dim as big_int_str ptr s

    s = big_int_str_create( 1 )
    if( s = NULL ) then
        exit sub
    end if
   
    if( big_int_to_str( num, 10, s ) <> 0 ) then
        exit sub
    end if
   
    print *s->str;
   
    big_int_str_destroy( s )
   
end sub

   
    dim as big_int ptr bignum

    bignum = big_int_create( 1 )

    big_int_from_int( 2, bignum )
   
    big_int_pow( bignum, 65536, bignum )
   
    print "2^65536 = ";
    print_num bignum
    print

    big_int_destroy( bignum )
coderJeff
Site Admin
Posts: 3410
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Postby coderJeff » Mar 29, 2006 1:01

2 ^ 256 =
115792089237316195423570985008687907853269984665640564039457584007913129639936

Code: Select all

Option Explicit
Const MaxDigits = 100

Type BigBase10
  d(0 To MaxDigits - 1) As Byte
End Type

Function b10_Zero(n As BigBase10)
  Dim i As Integer
  For i = 0 To MaxDigits - 1
    n.d(i) = 0
  Next i
End Function

Function b10_Inc(n As BigBase10)
  Dim i As Integer, c As Integer
  c = 1 '' carry register
  For i = 0 To MaxDigits - 1
    If c > 0 Then
      c = c + n.d(i)
      n.d(i) = c Mod 10
      c = (c - n.d(i)) \ 10
    Else
      Exit For  '' Early out
    End If
  Next i
End Function

Function b10_Add(dst As BigBase10, src As BigBase10)
  Dim i As Integer, c As Integer
  c = 0 '' carry register
  For i = 0 To MaxDigits - 1
    c = c + dst.d(i) + src.d(i)
    dst.d(i) = c Mod 10
    c = (c - dst.d(i)) \ 10
  Next i
End Function

Function b10_sprint(n As BigBase10) As String
  Dim i As Integer, x As String
  x = ""
  For i = MaxDigits - 1 To 0 Step -1
    If n.d(i) Or x > "" Then
      x = x & Chr(48 + n.d(i))
    End If
  Next i
  b10_sprint = x
End Function


Dim a As BigBase10, i as integer
b10_Zero a  '' set a to 0
b10_Inc a   '' set a to 1
For i = 1 To 256
  b10_Add a, a
Next i
Print b10_sprint(a)


EDIT: Or just use a a big numbers library. There are much faster ways to do these ops (32 bit instructions process 4 bytes at a time, data is packed, etc. did some code like this before all in C/asm. Thought I was going to crack RSAs ... not in this lifetime though)
v1ctor
Site Admin
Posts: 3804
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Postby v1ctor » Mar 29, 2006 1:14

Using GMP (it seems to take less than 1 sec to calculate 2^65536 in my box). The only problem is that it not easy to build GMP in Windows if you don't have experience with the GNU auto-tools:

Code: Select all

option explicit

#include once "gmp.bi"

sub print_num (byval num as mpz_ptr)
    dim as zstring ptr s

   s = mpz_get_str( 0, 10, num )
   
    print *s;
   
    deallocate( s )
   
end sub

   dim as mpz_ptr bignum = allocate( len( __mpz_struct ) )

   mpz_init_set_si( bignum, 2 )
   
   mpz_pow_ui( bignum, bignum, 65536 )
   
    print "2^65536 = ";
    print_num bignum
    print
   
   mpz_clear( bignum )
yetifoot
Posts: 1710
Joined: Sep 11, 2005 7:08
Location: England
Contact:

Postby yetifoot » Mar 29, 2006 1:43

2
Posts: 75
Joined: Mar 28, 2006 21:01

cha0s misunderstood.

Postby 2 » Mar 29, 2006 1:53

It gives this

1.15792089237316e+077

I know that is scientific notation,but it does not show all the digits.
Scientific notation is fine 4 powers of 10,but powers of 2
will NEVER end with a 0.

I'm gonna try what v1ctor posted.

BTW,I am not a total bright.I am quite a programmer,but very new 2
FreeBasic.I made this and it works,but there are faster ways.

Code: Select all


'This is a really demented way of calculating 2^65536,but it works.
'It's very slow.Please allow 32 seconds.
d=19729 'Digits required
c=65536 'Exponent
DIM a(d)
a(1) = 1
for b=1 to c
FOR y = 1 TO d
a(y) = a(y) * 2
NEXT
FOR z = 1 TO d
IF a(z) > 9 THEN
a(z) = a(z) - 10
a(z + 1) = a(z + 1) + 1
END IF
NEXT
if b=c then
FOR x = d TO 1 STEP -1
PRINT LTRIM$(STR$(a(x)));
NEXT
end if
next
sleep

[/code]
2
Posts: 75
Joined: Mar 28, 2006 21:01

Hey coderJeff,that's really cool.

Postby 2 » Mar 29, 2006 2:19

But I don't have any idea how it works.I never use subs(don't know how).
I never really found a use 4 them.

My example is so totally easy 2 understand.V1ctor had a cool
thing,but what's with all the subs?!

In JustBasic it's simple.If you enter

Code: Select all


print 2^65535



It's gonna print all the digits in about the same time.
So why would I even want 2 use FreeBasic? Well,it's
really cool in other ways,but lacks that 1 thing.

Please don't scold me 4 being a total bright.
I just want the shortest possible way of printing
numbers as high as 2^65536.As you saw from my site,I have tools
that can do it,but I really like FreeBasic because it's 1 of the few Basics
that actually compiles! Then I can send them an EXE.
I actually hope 1 day 2 get all the digits of 2^(2^32).

That means 2^4294967296.It hasn't done it in any tool I've ever found.
It's huge and I'll be VERY SHOCKED if even big_int can do it.

Return to “Beginners”

Who is online

Users browsing this forum: Google [Bot] and 11 guests