Is it possible to simplify this if statement?
and if so what's the answer?
if (type)
{
if(NdotL >= 0.0)
{
color += Idiff + Ispec;
}
}
else
{
color += Idiff + Ispec;
}
Think about this in terms of Boolean algebra. You have two conditions
A = (type)
B = (NdotL >= 0.0 )
And you execute your statement when
A * B
/A
( I use /A to indicate "NOT A", and * to indicate "AND" )
So the only time you don't execute is
A * /B
This means your statement should be
if (!((type) && NdotL < 0.0 )) {
// do your thing
}
Or, using Boolean identity
(A * B) = /(/A + /B)
you can rewrite your condition as
( /A + B )
if ( !(type) || ( NdotL >= 0 ) ) {
// do your thing
}
if (!type || NdotL >= 0.0)
{
color += Idiff + Ispec;
}
Use
if (type && NdotL > 0.0){
Blah....
} else {
Buegh...
}
Just so that it combines the two conditions.
Really sorry about indentations and such, but the mobile version of this site doesn't let you enter code, I just wanted to help you so much XD!
Try that :
color+=(type && NdotL >= 0.0)? Idiff + Ispec:Idiff + Ispec;
Related
I had a problem with this search algorithm, when the target value being the last element in the list, the middle becomes ZERO, where it must be 8, I calculated that by myself and I got 8, but when I do debug, I found zero again, if you can help, please feel free
this is the code :
class InterpolationSearch {
companion object{
fun Interpolation(list : List<Int> , Target_value : Int ){
var Lo = 0
var Hi = list.lastIndex
while (Lo <= Hi || list[Lo] <= list[Hi]){
var mid = Lo + ((Hi - Lo) / (list[Hi] - list[Lo])) * (Target_value - list[Lo]) // this is the middle.
if (list[mid] == Target_value){
println("success, target found in $mid")
break
}else if(list[mid] < Target_value){
Lo = mid + 1
}else if (list[mid] > Target_value){
Hi = mid -1
}
}
if (Lo >= Hi || list[Lo] >= list[Hi]){
println("failure, target not found")
}
}
}
}
fun main() {
val matrix = listOf<Int>( 32 , 33 , 54 , 1102 , 1124 , 1144 , 1150 , 1203 ,1222 )
try {
InterpolationSearch.Interpolation(matrix , 1222)
}catch (e : ArithmeticException){
println("SOMETHING DIVIDED BY ZERO :/")
}
}
set mid = Lo in case when Lo == Hi
I have a percentage calculation, but if no data is available, I get the following error message:
Division by zero
calculation
$ratio = ($postup*100)/($postup + $postdown);
Devision by zero is undefined.
If both $postup and $postdown are null (not set) you will get a division by zero i.e. null + null == 0.
Furthermore the same problem will occur if $postup * -1 == $postdown.
Since a division by zero is undefined you will need to add a fallback for this.
What this fallback would be is application specific but would look something like
$ratio = null;
if($postup + $postdown == 0) {
$ratio = xxx;
} else {
$ratio = ($postup*100)/($postup + $postdown);
}
Please also be aware that $postup * 100 will be equal to 0 if $postup == null
You can handle this, like the following to get rid of the error:
$ratio = 0;
$dividedBy = $postup + $postdown;
if($dividedBy != 0) {
$ratio = ($postup * 100) / $dividedBy;
echo $ratio;
} else {
echo "Can not divide by zero!!";
}
if ( x < 275 && x >= 0 )
f = 275
else if ( x < 450 && x >= 275) (right side comparison always previous left side default comparison value
f = 450
else if ( x < 700 && x >= 450)
f = 700
else if ( x < 1000 && x >= 700)
f = 1000
..... and more
is there any way or mathematical formula approach to eliminate this multiple if else statement for less code require?
Here is something in C (but easy enough to port to almost anything). It misses a couple of conditions (x < 0 and x > "biggest known") It doesn't add much value when you only have 4 values, but the more values you have, the more code this removes. Note that it will slow things down, but doubtful that you'd notice it unless you had a huge list of possible values.
int getF(int x)
{
/* possibleValues must be sorted in ascending order */
int possibleValues[] = { 275, 450, 700, 1000 };
for(int i = 0; i < sizeof(possibleValues) / sizeof(possibleValues[0]); i++)
{
int pv = possibleValues[i];
if (x < pv)
{
return pv;
}
}
/* fall thru "error" handle as you see fit */
return x;
}
if(x>0)
{
if(x<275)
f = 275;
else if(x<450)
f = 450;
else if(x<700)
f = 700;
else if(x<1000)
f = 1000;
else //and so on
Assuming that x>0. Just avoiding if-else, this also shall suffice the conditions -
x<275 ? 275 : (x<450 ? 450 : (x<700 ? 700 : (x<1000 ? 1000 :
Though if there is any defined type range of inputs like INT, BIG etc instead of 275, 450.. you can check the type of input. Also you can so the same using iteration as suggested by #John3136.
I need to validate a field in my form, this field belongs to the personal identification number of my country, this number has 10 digits
Example: card = 1710034065
2 1 2 1 2 1 2 1 2 (coefficient)
1 7 1 0 0 3 4 0 6 (personal identification number)
2 7 2 0 0 3 8 0 12 = 25 (Multiply each digit of the personal number by the
3 coefficient, if the result > 10 add between digits).
add multiplications
The result of the sum
25/10 = 2, Residue 5, divide 10 - residue 5 = 5 (check digit) ** which equals the last number of identity number**
Now I need is to implement this logic in the framework and I have no idea how,
I have a example code in java to get a better idea of what I need to do.
function check_cedula( form )
{
var cedula = form.cedula.value;
array = cedula.split( "" );
num = array.length;
if ( num == 10 )
{
total = 0;
digito = (array[9]*1);
for( i=0; i < (num-1); i++ )
{
mult = 0;
if ( ( i%2 ) != 0 ) {
total = total + ( array[i] * 1 );
}
else
{
mult = array[i] * 2;
if ( mult > 9 )
total = total + ( mult - 9 );
else
total = total + mult;
}
}
decena = total / 10;
decena = Math.floor( decena );
decena = ( decena + 1 ) * 10;
final = ( decena - total );
if ( ( final == 10 && digito == 0 ) || ( final == digito ) ) {
alert( "La c\xe9dula ES v\xe1lida!!!" );
return true;
}
else
{
alert( "La c\xe9dula NO es v\xe1lida!!!" );
return false;
}
}
else
{
alert("La c\xe9dula no puede tener menos de 10 d\xedgitos");
return false;
}
}
Let's say your model name is User and the field in the database is card, you would do the following;
<?php
class User extends AppModel {
/**
* Validation rules
*/
public $validate = array(
'card' => array(
'validateCard' => array(
'rule' => array('validateCard'),
'message' => 'Card does not validate'
)
)
);
/**
* Custom validation rule
* #return bool
*/
public function validateCard($field) {
$cardNumber = $field['card'];
// Here, perform your logic and return a boolean
}
}
Also, make sure in your view, you're using the FormHelper to output the form inputs and everything should play nice. For example;
<?php
echo $this->Form->create();
echo $this->Form->input('User.card');
echo $this->Form->end();
I have two functions for GNU bc in a Bash script.
BC_CEIL="define ceil(x) { if (x>0) { if (x%1>0) return x+(1-(x%1)) else return x } else return -1*floor(-1*x) }\n"
BC_FLOOR="define floor(x) { if (x>0) return x-(x%1) else return -1*ceil(-1*x) }\n"
echo -e "scale=2"$BC_CEIL$BC_FLOOR"ceil(2.5)" | bc
Both functions work fine in interactive bc. bc does not seem to allow multiple functions on one line separated by ; though, so I have to echo -n | bc with newlines at the end of each function. The above output is 2.5, not the expected 3.0 that I get if I type it into bc -i myself. It seems that bash calls bc for each line of echo output, rather than echo'ing it all to a single instance. Is there any workaround for this?
The scale needs to be zero for x%1 to work. You should normally only have one return from a function.
define ceil(x) { auto savescale; savescale = scale; scale = 0; if (x>0) { if (x%1>0) result = x+(1-(x%1)) else result = x } else result = -1*floor(-1*x); scale = savescale; return result }
define floor(x) { auto savescale; savescale = scale; scale = 0; if (x>0) result = x-(x%1) else result = -1*ceil(-1*x); scale = savescale; return result }
This needs a newline after the scale statement:
echo -e "scale=2\n"$BC_CEIL$BC_FLOOR"ceil(2.5)" | bc
I believe 1. is incorrect.
The if() comparison needs to be X >= 0 .
I find this works
define ceil(x) {
if (x >= 0) { if (x%1>0) return x+(1-(x%1)) else return x }
else return -1*floor(-1*x)
}
define floor(x) {
if (x >= 0) return x-(x%1)
else return -1*ceil(-1*x)
}