I have a question about the operator precedence in PB is it the same in C (c99) or are they different?
working on porting some code I cant get it to work properly even with a literal port on very basic code
im thinking the operator precedence might be different
I have multiplications followed by additions followed by multiplications, does anyone know if the precedence is the same in PB? thanks
Operator precedence in C and PB
-
- Enthusiast
- Posts: 480
- Joined: Thu Jul 27, 2006 4:06 am
Operator precedence in C and PB


Re: Operator precedence in C and PB
What happens if you use brackets to force the correct order? Or can you post the calculation?
The order precedence in PureBasic is as follows (from https://www.purebasic.com/documentation ... ables.html):
The order precedence in PureBasic is as follows (from https://www.purebasic.com/documentation ... ables.html):
Code: Select all
Priority Level | Operators
---------------+---------------------------
8 (high) | ~, - (negative)
7 | <<, >>, %, !
6 | |, &
5 | *, /
4 | +, - (substraction)
3 | >, >=, =>, <, <=, =<, =, <>
2 | Not
1 (low) | And, Or, XOr
-
- Enthusiast
- Posts: 581
- Joined: Wed Sep 25, 2019 10:18 am
-
- Enthusiast
- Posts: 480
- Joined: Thu Jul 27, 2006 4:06 am
Re: Operator precedence in C and PB
oh just a matrix translation
but I get different results from pb and c
the pb code without any brackets works fine:
when in c the same code gives a different result so i assumed it was due to the precedence
now that i think about it this seems like a C question but I'm intrigued and there are very few places to talk about PureBasic and if anyone knows it ought to be here!
not sure it matters but i pass the structure in C as: struct MATRIX *matrix (i know my naming convention is awful)
i had other problems that i solved with the C alignment option before this so im thankful for the manual on that one!
maybe the problem lies elsewhere?
but I get different results from pb and c
the pb code without any brackets works fine:
Code: Select all
*matrix\e41 = *matrix\e11 * x + *matrix\e21 * y + *matrix\e31 * z + *matrix\e41
*matrix\e42 = *matrix\e12 * x + *matrix\e22 * y + *matrix\e32 * z + *matrix\e42
*matrix\e43 = *matrix\e13 * x + *matrix\e23 * y + *matrix\e33 * z + *matrix\e43
Code: Select all
matrix->e41 = matrix->e11 * x + matrix->e21 * y + matrix->e31 * z + matrix->e41;
matrix->e42 = matrix->e12 * x + matrix->e22 * y + matrix->e32 * z + matrix->e42;
matrix->e43 = matrix->e13 * x + matrix->e23 * y + matrix->e33 * z + matrix->e43;
not sure it matters but i pass the structure in C as: struct MATRIX *matrix (i know my naming convention is awful)
i had other problems that i solved with the C alignment option before this so im thankful for the manual on that one!
maybe the problem lies elsewhere?


-
- Enthusiast
- Posts: 581
- Joined: Wed Sep 25, 2019 10:18 am
Re: Operator precedence in C and PB
Code: Select all
; matrix sample
Structure matrixtype : e11.d : e12.d : e13.d : e21.d : e22.d : e23.d : e31.d : e32.d : e33.d : e41.d : e42.d : e43.d : EndStructure
Define *matrix.matrixtype=AllocateStructure(matrixtype)
Define x.d=1.0 : y.d=2.0 : z.d=3.0
*matrix\e11=1.1 : *matrix\e12=1.2 : *matrix\e13=1.3
*matrix\e21=2.1 : *matrix\e22=2.2 : *matrix\e23=2.3
*matrix\e31=3.1 : *matrix\e32=3.2 : *matrix\e33=3.3
*matrix\e41=4.1 : *matrix\e42=4.2 : *matrix\e43=4.3
*matrix\e41 = *matrix\e11 * x + *matrix\e21 * y + *matrix\e31 * z + *matrix\e41
*matrix\e42 = *matrix\e12 * x + *matrix\e22 * y + *matrix\e32 * z + *matrix\e42
*matrix\e43 = *matrix\e13 * x + *matrix\e23 * y + *matrix\e33 * z + *matrix\e43
s.s+";"+StrD(*matrix\e41)+" "+StrD(*matrix\e42)+" "+StrD(*matrix\e43)+#CRLF$
*matrix\e41=4.1 : *matrix\e42=4.2 : *matrix\e43=4.3
! p_matrix->f_e41 = p_matrix->f_e11 * v_x + p_matrix->f_e21 * v_y + p_matrix->f_e31 * v_z + p_matrix->f_e41;
! p_matrix->f_e42 = p_matrix->f_e12 * v_x + p_matrix->f_e22 * v_y + p_matrix->f_e32 * v_z + p_matrix->f_e42;
! p_matrix->f_e43 = p_matrix->f_e13 * v_x + p_matrix->f_e23 * v_y + p_matrix->f_e33 * v_z + p_matrix->f_e43;
s+";"+StrD(*matrix\e41)+" "+StrD(*matrix\e42)+" "+StrD(*matrix\e43)+#CRLF$
s+ "; Compiler Version: "+Str(#PB_Compiler_Version)
If #PB_Compiler_Processor=#PB_Processor_x86
s+" x86 "
ElseIf #PB_Compiler_Processor=#PB_Processor_x64
s+" x64 "
EndIf
s+"OSVersion(): "+Str(OSVersion())
SetClipboardText(s)
CompilerIf Not Defined(PB_Compiler_Backend,#PB_Constant)
CompilerError "Please use PB Version>=6.00"
CompilerElseIf #PB_Compiler_Backend<>#PB_Backend_C
CompilerError "C Backend Code Only"
CompilerEndIf
;18.7 19.4 20.1
;18.7 19.4 20.1
; Compiler Version: 600 x64 OSVersion(): 2000