question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Compiling and assigning result of multiline function

See original GitHub issue

Hi! I’m trying to move from Mathcad to Calcpad, since this is an amazing and fast software. I have a function that has an if condition inside for calculating an effective moment of inertia. This is what I want to do in Calcpad:

image

And this is what I managed to do:

image

But I can’t assign the function result to a variable when compiling. This is what I get: image And if I put it in the same line: image image

I would like to run this function for many values of Ma and assign it to different variables or just the function inside an equation, like this: image

What I managed to do is this:

#def Ie$(Mcr$; Ma$)
#if Ma$ ≤ 2/3*Mcr$
Ie = I_g
#else
Ie = I_cr/(1 - (2/3*Mcr$/Ma$)^2*(1 - I_cr/I_g))
#end if
#end def

Ie$(M_cr; M_pp)
I_e.pp = Ie

It does the trick but doesn’t look quite good. And I can’t get to hide the result of the if equation with #hide and #show. image

Also, is there a way to make AutoRun work when working with functions? Because now I have to compile the inputs and then the outputs. Thanks for the help!

Issue Analytics

  • State:closed
  • Created 7 months ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Proektsoftbgcommented, Feb 21, 2023

As @hildebrandopsj already replied, you should use the #if statement instead.

In general, math functions do not work with strings, but you can define string functions, called “macros”. Their names must end with $. For example, you can create an inline function, similar to if() to handle your case:

#def if$(cond$; true$; false$)
    #if cond$
        'true$
    #else
        'false$
    #end If
#end def

Then, you can use it like that:

r = 0.5
if$(r < 1; Verifica; No verifica)

image

Macros are very powerful to mix strings with expressions. You can use them to create your own macro extension to the Calcpad language that automates specific tasks. For example, I made this to easy draw svg images with lines, circles, texts and dimension lines:

https://gist.github.com/Proektsoftbg/1f16d0f85e1007266455177f159eef23

And this is how the result looks:

image

The drawing is parametrically dependent on the input data, not just a static picture. I am also experimenting with generation of AutoCAD script files from Calcpad in a similar way.

2reactions
Proektsoftbgcommented, Feb 19, 2023

Hi, use the If() function: f(x; y) = if(x<y; x; y)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I assign the result of a function on multiple lines? Python
Ideally, I would like to use the line breaker \ , e.g. ... However, I don't think this is possible with the result...
Read more >
Python - Multi-Line Statements
In Python, a statement is a logical command that a Python interpreter can read and carry out. It might be an assignment statement...
Read more >
How to assign a string value to a variable over multiple ...
Here the issue is that you are surrounding the variable with double quotes (""). Remove it and things will work fine.
Read more >
Multi-Line Statements in Python
There are two types of statements in Python. They are assignment statements and expression statements. Both of them can be broken into multiple ......
Read more >
linux - How do I pass a multiline variable as a parameter to ...
Trying to capture and display the output of curl was driving me nuts. I finally found a workaround, but I don't understand why...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found