Preventing Divide by Zero, problem with IF?
-
Hi All
I'm a comparative newbie to sketchup and complete newbie to this forum. I have searched this forum as best I can and have found a couple of other people who have a similar problem but no solution. I'm hoping someone out there has worked this out.
I have a simple component with a single custom attribute - angle.
I then use LenX/TAN(angle) to work out LenY
This works fine except when angle=90, because TAN(90) is undefined.
In this case I would like to set LenY=0
So I tried something like
LenY = IF(angle=90,0,LenX/TAN(angle))
and
LenY = IF(angle<90,LenX/TAN(angle),0)
In both cases sketchup evaluates the part of the IF statement not required and causes an error.
I have solved this problem by evaluating the other angle in the triangle
ie
LenY = LenX*TAN(90-angle)
but I wonder why the IF method didn't work and if anyone out there has any alternative methods to implement this sort of defensive programming to prevent divide by zero
IF(attrib1=0,0,attrib2/attrib1)
I know others have the same problem, surely this should be easy
anyway thanxs in advance, I love Sketchup and this site looks great!
-
Hello,
I had the same problem with a A/sin(B) when B = 0.
I turned and turned to found a solution...
Look at this thread -
daredevil
yeh I saw your bridge, very impressive work. My models are a lot more humble but I aspire to create complex dynamic components. That is the reason why I would like to find out how to do these things nicely and elegantly.
I presume the IF problem we both had is a problem with the way sketchup parses the IF function. I have a little experience with C++ from 20 years ago and would have expected it to work.
Anyway I am wondering if I should learn Ruby and if these sort of things can be done in that. I am frustrated with the way you enter formulas in the dynamic component attributes dialogue, it works fine for simple components but it is difficult to read complex nested functions in such a small text box. Sometimes I have resorted to copy/paste into notepad, and use indentation to help match parenthesis and make it clearer and then copy/paste back. It would also be nice to be able to add comments.
From seeing the bridge you've made you're obviously well ahead of me. I would like to produce fully configurable pieces of furniture and joinery for my business. Do you think I should persevere with Dynamic Components or will I discover that their limitations mean I should learn the Sketchup Ruby API?
Again very impressive work you do, it would give me a real kick to produce something half as good as that

Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register LoginAdvertisement