Write decimal as closest "reasonable" fraction

Can someone recommend some reading or direct tips on writing long/complicated math expressions in Julia code?

  • What to do if something is too long to fit on one line?
  • What to do if something has too many parentheses to be readable? Is there a way to get larger parentheses (like in latex) or use or delimiters like brackets?
  • Are there good ways to substitute a variable for a complex and frequently repeated expression? obviously, this can be done if the expression is just constant, but if there are variables inside the expression it gets tricky.

I’m not gonna give examples because I’m asking for general tips on this sort of thing or if there is a style guide to read. Thanks

You break it into several lines, like you would if you were writing equations in LaTeX. Use indentation to align things nicely.

No. I would either break it into several lines, or assign one or more sub-expressions into variables that I later plug into the main expression.

I’m unsure whether I understand the question, but you could perhaps use functions for this.

2 Likes

Most of these points can probably be addressed by splitting your expression into multiple smaller functions. Different sized delimeters would be a feature of your text editor/IDE (though I haven’t heard of an editor with that option). I like rainbow delimeters, which colors each matching pair.

As for long expressions, you can break lines anywhere that Julia won’t think the expression is complete. So

x = x^2 + x + 1

is equivalent to

x = x^2 +
    x +
    1

but

x = x^2
    + x
    + 1

will be interpreted as three separate expressions.

As DNF already mentioned functions may help. They can be defined everywhere and in one line, e.g.:

smn(n) = (n + 1)n / 2.0
q = (smn(7) + smn(8)) * 19