If statement issue with # divided by -0
-
I have a stretchable surface that I'm dividing in the x and y direction through two user inputs.
If the user enters "5" then I draw file dividing lines (including the two edges) so I end up with four gaps. So to get the correct measurement I take LenX/(n-1). This works great unless the user's original number was 1. The desired result if the user enters "1" is to just have a line at one edge.
Of course if I leave things as they are I get an error because I'm dividing by 0. So I created an If statment something like this
if(n=1,do this,LenX/(n-1)). So what is odd, is that when I have a value of 1 and should just "do this" I still get an error about the Div/0. I'm not sure why. Has anyone else run across this.
-
i'm not sure i get your point or question. if my memory serves me right any number divided by zero is infinite. try running that into your calculator or excel and you will get an error.
-
I think David's problem is that it still divides by zero even after he's created an if() statement to try and get around the issue.
-
if(n=**
**=**
**1,do this,LenX/(n-1)) ? -
@unknownuser said:
if(n==1,do this,LenX/(n-1)) ?
what is the double equals for? SketchUp doesn't seem to like "==" in that formula.
-
== is the 'comparison' symbol in ruby, so i guess chris was thinking that DCs might prefer this operator to '='.
As fa as i know, DCs are designed to behave like a spreadsheet in terms of notaion, so i think the '=' is correct. Dont know whats causing the problem though.
-
Yes that's why I put a ? Seems odd that DC's should use different operators than ruby and javascript (= is used to assign a value to a variable; == or === to test true or false) ... but definitely the if part is being ignored unless I'm really getting senile.
-
FYI - I just added a file to this. It is a sample version of Sang with three Dynamic Attributes.
TestFormula = =if(usernumber=1,"you are 1",usernumber/(usernumber-1))
anotherformula = =if(usernumber=1,"you are 1","you are not 1")
usernumber = (user input)Each formula has the same formula except for the "false" case which is where the div0 is in the "Test Formula). Why does it care what is in that until it fails the "usernumber!=1" test.
So the IF isn't being ignored unless I happen to have a value that makes a Div0. It is as if it checks for that before it bothers reading the formula. If there aren't any Div0's then it correctly uses the If statement like it should.
-
Hi David -
I've noticed that sometimes, if the very first time a formula is assessed, the result is zero, I get an error. I don't know if this could be the issue here.
If n=1, does the formula return zero?
My work around is to write a formula that never returns zero, and then let the DC evaluate it. Once it has accepted a non-zero value, I can edit the formula to allow a zero value, and it seems to work fine. (An example of when this comes up is a RotX, RotY, or RotZ value that is set by a user input box; I initially get an error if the value in the box is zero, but if I start with a non-zero value in the user input box when I create the formula in the RotY attribute, it works fine, even if I set it back to zero.)
I don't know if this is related to what you're doing here, but it may be worth a shot if you have the result of the formula set to zero when n=1.
Advertisement