More simple math functions...

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

More simple math functions...

Post by Lebostein »

In every basic language in this world (apart form PB) i found follow simple functions:

Abs(x) - returns the absolute value of a number (for integers)
Swap(x,y) - exchanges the values of the variables
Sign(x) - returns a positive, zero, or negative integer based on the sign of argument

please implement this simple functions. Thanks!


PS: Why not exists a konstant of "PI", like #PI = 3.14159265359?
FloHimself
Enthusiast
Enthusiast
Posts: 229
Joined: Wed May 14, 2003 3:38 pm
Location: Lüneburg - Germany

Post by FloHimself »

#PI = 3.14159265359
Numeric overflow: too big number :P
User avatar
Inner
PureBasic Expert
PureBasic Expert
Posts: 714
Joined: Fri Apr 25, 2003 4:47 pm
Location: New Zealand

Post by Inner »

Abs() exists.
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Post by IceSoft »

FloHimself wrote:
#PI = 3.14159265359
Numeric overflow: too big number :P
Not with PB 4.0 :wink:
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
FloHimself
Enthusiast
Enthusiast
Posts: 229
Joined: Wed May 14, 2003 3:38 pm
Location: Lüneburg - Germany

Post by FloHimself »

see your signature! :P
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

Post by Lebostein »

Inner wrote:Abs() exists.
..only for float numbers!!! Read the manual accurately and you see: "This function works correctly only with float numbers. With integer it will fail if the integer is too big (loss of precision). Another function will be added to do that." (http://www.purebasic.com/documentation/index.html)

The label "Abs()" for this function is actual confusing, "AbsF()" is better - like Str() and StrF().

Hehe, sorry for my PI:

Code: Select all

#PI = 3.14159265
Debug #PI
Andras
User
User
Posts: 34
Joined: Wed Oct 27, 2004 6:58 am
Location: Germany

Post by Andras »

Pi=ATan(1)*4
Undank ist der Welten Lohn
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

Andras wrote:Pi=ATan(1)*4

Code: Select all

pi.f

Pi=ATan(1)*4

Debug pi
;)

- np
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Lebostein wrote:The label "Abs()" for this function is actual confusing, "AbsF()" is better - like Str() and StrF().

Hehe, sorry for my PI:

Code: Select all

#PI = 3.14159265
Debug #PI
You are right about the syntax incoherence. I second that.

As you can see #PI constant sould be better to be stablished when 64, 80, (and 128) bit floats are available.
Inner wrote:Abs() exists.
Yeah. Just like God. But not for integers, but for fractionated ones. :P :wink:

BTW, shorter:

Code: Select all

Pi=ACos(-1)
:wink:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

LOL

Code: Select all

; The result will be output to approximately the number of decimal places requested 
; Initially try 770 decimal places.  The last 8 digits should be 9999 9983 

Declare addbtoa() 
Declare subbfroma() 
Declare divcby25() 
Declare divdby57121() 
Declare divcbyn() 
Declare divdbyn() 

Global d, x, n 

OpenConsole() 
Print("Number of decimal places? ") 
np=Val(Input()) 
If np<4: np=4: EndIf 
PrintN("") 
x=10000 : xw=4 
d=np/xw+2 
; Working registers, each element holding an integer 0..9999 
Dim a(d) 
Dim b(d) 
Dim c(d) 
Dim d(d) 
it=(np/Log10(25)+5)/2 ; Number of iterations 
c(1) = 800 : d(1) = 9560 : n=-1 
For j=1 To it 
  n + 2 
  divcby25() 
  divcbyn() 
  addbtoa() 
  divdby57121() 
  divdbyn() 
  subbfroma() 
  n + 2 
  divcby25() 
  divcbyn() 
  subbfroma() 
  divdby57121() 
  divdbyn() 
  addbtoa() 
Next j 
; Print result 
Print("  3.1") 
For i=2 To d-1 
  Print(RSet(Str(a(i)),xw,"0")+" ") 
  If (i%15)=0 
    PrintN("") 
  EndIf 
Next i 
PrintN("") 
PrintN("Press ENTER") 
Input() 
CloseConsole() 
End 


Procedure addbtoa() ; Multi-precision addition 
  c=0 ; Carry 
  For i=d To 1 Step -1 
    s = a(i)+b(i)+c 
    c = s/x 
    a(i) = s%x 
  Next i 
EndProcedure 

Procedure subbfroma() 
  c = 0 
  For i=d To 1 Step -1 
    s = a(i)-b(i)-c 
    If s<0 
      s + x: c = 1 
    Else 
      c=0 
    EndIf 
    a(i) = s%x 
  Next i 
EndProcedure 

Procedure divcby25() 
  r = 0 ; Remainder 
  For i=1 To d 
    c = c(i)+x*r 
    c(i) = c/25 
    r=c%25 
  Next i 
EndProcedure 

Procedure divdby57121() 
  r = 0 
  For i=1 To d 
    c = d(i)+x*r 
    d(i) = c/57121 
    r=c%57121 
  Next i 
EndProcedure 

Procedure divcbyn() 
  ; b()=c()/n 
  r = 0 
  For i=1 To d 
    c = c(i)+x*r 
    b(i) = c/n 
    r=c%n 
  Next i 
EndProcedure 

Procedure divdbyn() 
  ; b()=d()/n 
  r = 0 
  For i=1 To d 
    c = d(i)+x*r 
    b(i) = c/n 
    r=c%n 
  Next i 
EndProcedure 
- np
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Code: Select all

Procedure AbsL(Value.l)
  If Value < 0 : Value = - Value : EndIf
  ProcedureReturn Value
EndProcedure
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

Post by Lebostein »

ts-soft wrote:

Code: Select all

Procedure AbsL(Value.l)
  If Value < 0 : Value = - Value : EndIf
  ProcedureReturn Value
EndProcedure
This is a wishlist forum and not a "how i can do...?". My ambition (vision) is a better Basic with all standard functions from the older/other Basics. I don't need this functions this time, I will reduce the work to convert code from other Basics to PB, for example.

These functions are very simple to implement.... I hope against hope :D
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Lebostein wrote:This is a wishlist forum and not a "how i can do...?". My ambition (vision) is a better Basic with all standard functions from the older/other Basics. I don't need this functions this time, I will reduce the work to convert code from other Basics to PB, for example.

These functions are very simple to implement.... I hope against hope :D
Well said Lebostein. Great. And I second your ambition for PB too :) :wink:
I think just that, and some other things, is what all the PB comunity member must want and try in order to get a better and better PB. :!:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

Post by Lebostein »

Now, after 3 years since the last request, I see no change. I am very unhappy and discontented about the situation of the math library in PB! What is the problem to add this simple functions?

:( :x :roll:
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

No need to dig up old posts and tell "man what are you doing here, it's alreay XX years and nothing comes" - it's annoying. This forum is for features request, not things to implement. If you are not happy with our decisions, write your own functions. BTW, 'swap' is here or i miss something ?
Post Reply