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)
}
Related
i am trying to get the color of a pixel below a player and if it is blue i stop them from falling down. for some reason it detects the color but the if statement never runs even though it matches. here is code:
gravity(){
if (this.y <= 400 - this.scale){
let gc = get(this.x, this.y + 25);
print(gc);
if (gc == [0, 0, 255, 255]){
this.canGravity = false;
}
else {
this.canGravity = true;
}
if (this.canGravity == true){
this.y += 2;
}
}
}
You're very close, your issue is the equality check.
Take a look at the following example:
let first = [1, 2];
let second = [1, 2];
console.log(first === second); // false;
The best way to check for equality of two arrays in JavaScript is to loop through each element and perform a comparison, so your example might look like:
if (arraysEqual(gc, [0, 0, 255, 255])) {
this.canGravity = false;
}
...
function arraysEqual(arr1, arr2) {
return arr1.length === arr2.length && arr1.every((value, index) => value === arr2[index]);
}
Or even simpler for your scenario:
if (gc[0] === 0 && gc[1] === 0 && gc[2] === 255 && gc[3] === 255) {
...
}
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.
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;
I'm having a bit of a brain fart on making this code more concise(preferably a single boolean expression)
This is my code:
if (d.Unemployed)
{
if (type.Unemployed)
{
tmp.Unemployed = true;
}
else
{
tmp.Unemployed = false;
}
}
else
{
if (type.Unemployed)
{
tmp.Unemployed = false;
}
else
{
tmp.Unemployed = true;
}
}
Basically the point is that if either type or d is not unemployed, then tmp should be set to not unemployed.
How about:
tmp.Unemployed = type.Unemployed == d.Unemployed;
If we construct a truth table by following the code, we get
d | type | tmp
---+------+----
1 | 1 | 1
---+------+----
1 | 0 | 0
----+-----+----
0 | 1 | 0
----+-----+----
0 | 0 | 1
The above is equivalent with the negation of the xor operation.
tmp = not (d xor type)
If the language doesn't have the xor operator we can use the != on boolean values.
tmp = ! (d != type);
// or
tmp = d == type;
Thinking about how much "brain fart" this caused you I would consider using a well named variable to avoid having to go through this mental process again in future. Something like this:
isTmpUnemployed = (type.Unemployed == d.Unemployed);
tmp.Unemployed = isTmpUnemployed;
The above code means "both unemployed or both not unemployed". Thus, not (A xor B):
tmp.Unemployed = ! ( D.Unemployed ^ type.Unemployed)
tmp.Unemployed = d.Unemployed || type.Unemployed ? !tmp.Unemployed : null;