QB64 Phoenix Edition
Ternary operator - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11)
+--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2)
+--- Thread: Ternary operator (/showthread.php?tid=2130)



Ternary operator - Kernelpanic - 10-30-2023

Can one also integrate the declaration of a library into a function?
The reason for the question is that I wrote a small program that allows one to use the tenary operator from C. If you could put the whole thing into a function, it might be useful for Basic. Maybe.  Huh

The Basic-Program:
Code: (Select All)

'Aufruf des tenaeren Operators in C - 30. Okt. 2023

Option _Explicit

'In QB64 mit "Declare Library" wie angegeben
Declare Library "D:\Lab\QuickBasic64\Extern-nicht-Basic\C-Aufruf\tenaerOp"
  Function tenaerOp& (ByVal N As Long, Byval X As Long)
End Declare

Dim As Long N, X

X = 77

Locate 3, 3
Input "Zahl: ", N

Locate 5, 3
Print Using "Ausgabe: ###"; tenaerOp&(N&, X&)

End

The C part:
Code: (Select All)
//TenaerenOperator von Basic aufrufen - 30. Okt. 2023

#include <stdio.h>
#include <stdlib.h>

long tenaerOp(long eingabe, long x)
{
    long ausgabe;
        
    ausgabe = x > eingabe ? 100 : 200;
    return(ausgabe);    
}



RE: Ternary operator - DSMan195276 - 10-30-2023

Could you clarify what you're trying to achieve? You don't need C to write that function, the ternary operator is the same as just using an `if` for your usage:

Code: (Select All)
Function tenaerOp&(e As long, x As Long)
    If x > e Then
        tenaerOp& = 100
    else
        tenaerOp& = 200
    End If
End Function

Unfortunately if you want a "real" ternary operator you can't implement that as a function. The ternary operator is only supposed to evaluate one of the true or false paths, not both, but there's no way to achieve that with a function call.


RE: Ternary operator - bplus - 10-30-2023

Code: (Select All)
Print Iff$(_Width > _Height, "Landscape View", "Portrait View")
x = 150: e = 200
Print Iff$(x > e, "100", "200")

Function Iff$ (Bool&, true$, false$)
    If Bool& <> 0 Then Iff$ = true$ Else Iff$ = false$
End Function



RE: Ternary operator - Kernelpanic - 10-30-2023

I know about "If", but in C, Java etc. the tenary operator is there not for fun.

Again: Can something like this be implemented in a function in QB64?


RE: Ternary operator - DSMan195276 - 10-31-2023

(10-30-2023, 11:50 PM)Kernelpanic Wrote: Again: Can something like this be implemented in a function in QB64?
Not in the way it behaves in languages like C and Java, it would have to be built into the language to work properly. Ex. In C and Java you can do stuff like this:

Code: (Select All)
x = (a != 0)? 20 / a: 0; // Checks that a is not zero before dividing by it
There's no way to implement that correctly with a function. The `20 / a` in the ternary operator is only supposed to be evaluated if `a != 0` is true, but with functions all the arguments are evaluated before the function is called.


RE: Ternary operator - bplus - 10-31-2023

Code: (Select All)
For a = -1 To 1
    x = Val(Iff$(a <> 0, _Trim$(Str$(20 / a)), "0"))
    Print x
Next

Function Iff$ (Bool&, true$, false$)
    If Bool& <> 0 Then Iff$ = true$ Else Iff$ = false$
End Function



RE: Ternary operator - DSMan195276 - 10-31-2023

(10-31-2023, 01:37 AM)bplus Wrote:
Code: (Select All)
For a = -1 To 1
    x = Val(Iff$(a <> 0, _Trim$(Str$(20 / a)), "0"))
    Print x
Next

Function Iff$ (Bool&, true$, false$)
    If Bool& <> 0 Then Iff$ = true$ Else Iff$ = false$
End Function
Try it again, but skip the floating point math (where division by zero is technically allowed). Do `20 \ a` and the program always explodes, since it gets evaluated before calling the `Iff$()` function.


RE: Ternary operator - bplus - 10-31-2023

Got it! Thanks for the good example.


RE: Ternary operator - Kernelpanic - 10-31-2023

(10-31-2023, 01:13 AM)DSMan195276 Wrote:
(10-30-2023, 11:50 PM)Kernelpanic Wrote: Again: Can something like this be implemented in a function in QB64?
Not in the way it behaves in languages like C and Java, it would have to be built into the language to work properly. Ex. In C and Java you can do stuff like this:

Code: (Select All)
x = (a != 0)? 20 / a: 0; // Checks that a is not zero before dividing by it
There's no way to implement that correctly with a function. The `20 / a` in the ternary operator is only supposed to be evaluated if `a != 0` is true, but with functions all the arguments are evaluated before the function is called.
Yes, that obviously does not work. How should such and similar variations be intercepted?
Gets kind of complicated.

Code: (Select All)

'Tenaerer Operator , Problem in Funktion - 31. okt 2023
'x = (a != 0)? 20 / a: 0; // Checks that a is not zero before dividing by it

Option _Explicit

Declare Function tenaererOperator(a As Double, b As Double) As Doeble

Dim As Double a, b, x

Locate 3, 3
Input "Zahl a: ", a

Locate 4, 3
Input "Zahl b: ", b

If a <> 0 Then
  x = b / a
Else
  x = 0
End If

Locate 7, 3
Print Using "b / a = ##.####"; x

Locate 9, 3
If tenaererOperator(a, b) = -1 Then
  Print "Keine negativen Werte!"
ElseIf tenaererOperator(a, b) = -2 Then
  Print "Division durch Null!"
Else
  Print Using "x = ###.####"; tenaererOperator(a, b)
End If

End

Function tenaererOperator (a As Double, b As Double)

  Dim As Double x

  If a <> 0 And Sgn(a) = 1 And Sgn(b) = 1 Then
    x = b / a
  ElseIf a <> 0 And Sgn(a) = -1 Then
    x = -1
  ElseIf a <> 0 And b = 0 Then
    x = -2
  Else
    x = 0
  End If

  tenaererOperator = x
End Function