Jump to content

If statement representation


ucznmsb

Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

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.