More simple math functions...
More simple math functions...
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?
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?
-
- Enthusiast
- Posts: 229
- Joined: Wed May 14, 2003 3:38 pm
- Location: Lüneburg - Germany
-
- Enthusiast
- Posts: 229
- Joined: Wed May 14, 2003 3:38 pm
- Location: Lüneburg - Germany
..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)Inner wrote:Abs() exists.
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
- NoahPhense
- Addict
- Posts: 1999
- Joined: Thu Oct 16, 2003 8:30 pm
- Location: North Florida
- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
You are right about the syntax incoherence. I second that.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
As you can see #PI constant sould be better to be stablished when 64, 80, (and 128) bit floats are available.
Yeah. Just like God. But not for integers, but for fractionated ones.Inner wrote:Abs() exists.


BTW, shorter:
Code: Select all
Pi=ACos(-1)

- NoahPhense
- Addict
- Posts: 1999
- Joined: Thu Oct 16, 2003 8:30 pm
- Location: North Florida
LOL
- np
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
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.ts-soft wrote:Code: Select all
Procedure AbsL(Value.l) If Value < 0 : Value = - Value : EndIf ProcedureReturn Value EndProcedure
These functions are very simple to implement.... I hope against hope

- Psychophanta
- Always Here
- Posts: 5153
- Joined: Wed Jun 11, 2003 9:33 pm
- Location: Anare
- Contact:
Well said Lebostein. Great. And I second your ambition for PB tooLebostein 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


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.
