Jump to content

If statement representation

Featured Replies

Apologies, if I have put this in the wrong section but I don't know enough about Maths to know what kind of question it is!

I need to represent an IF statement formula in an alternative way as the software I am using doesn't allow IF statements. It does however allow all of these functions: http://docs.moodle.org/20/en/grade/edit/tree/calculation

I need a formula that doesn't use IF but will produce the same results as the one below:

IFAverage<1 Final Mark = Average*50

IFAverage>=1and<2 Final Mark = 50+(10*Average)

IFAverage>=2and<3 Final Mark = 60+(10*(Average - 2))

IFAverage>=3 Final Mark = Average*25

So I want to be able apply this formula to my average mark. So if my average mark was 2.14 then the result I would get after applying the formula would be 61.43.
If it was 3.29, my result would be 82.25.

I have done some reading and people seem to think that most IF formulas can be calculated using ROUND instead but I wouldn't know where to begin. Also,
I don't care how convoluted or long the formula is as noone will ever see it.

Many thanks in advance

If you have code:

 

if( x > y )

{

z = x * a;

}

else

{

z = x * b

}

 

You can replace it by f.e.

 

z = ( x > y ) ? ( x * a ) : ( x * b );

 

or simpler

 

z = x * ( ( x > y ) ? a : b );

 

 

Comparison operators > or < can be replaced by subtraction (they're subtraction in fact in CPU, but result is forgotten and remaining is only CPU flag register).

 

So, ( x > y )

can be replaced by

a=x-y;

and then you have to check whether a is positive or negative value, or equal 0 (x is equal y).


If value is negative sgn() or sign() function will return -1.

If value is positive sgn() or sign() function will return +1.

If value is 0, it'll return 0.


So,

 

if( x > y )

 

can be replaced by:

 

sgn( x - y )

 

 

max( x,0 ) will return 1 only when x is equal 1.

max( -x, 0 ) will return 1 only when x is equal -1.

(assuming x is result of sgn())

 

So whole equation should looks like:

 

( max( sgn( x - y ), 0 ) * ( x * 100 ) ) + ( max( -sgn( x - y ), 0 ) * ( x * 50 ) )

 

which should be equivalent to

if( x > y ) z = x * 100;

else if( x < y ) z = x * 50;


I hope so I inspired you a bit.

Edited by Sensei

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.