system
Given the following post, respond as if you're commenting on Reddit:
human
Better one statement with all calculations are multiple statements with easier calculations?

I'm learning c# and working on a little project i have been thinking about for a while. In it variables are influenced by different inputs and calculations. I was wondering for running more smoothly, is it better to update the variable in different lines of code with easier calculations, or to have one line of code that combines all the influences and combinations?

ex1: variable1 = variable1 + variable2\*3.1415;

variable1 = variable1- math.pow(variable3,2)

variable 1 = ...

&#x200B;

ex2: variable1 = variable1 + variable2\*3.15-math.pow(variable3,2)...
gpt
Readability should be your first priority, especially when creating the first version and debugging. Once you have it working *then* think about optimising the details\* of the calculations if they are running too slowly for your needs (and ideally actually profile them, since the compiler will be optimising behind the scenes anyway).

One other thing to consider is how the order of your calculations affects precision. In floating point (a + b) + c may not equal a + (b + c), especially if the numbers have a wide range of exponents. A classic example is:

    (1 + 1e20) - 1e20
    1 + (1e20 - 1e20)

\*you should obviously think about picking a good *algorithm* before you start, but that's a different issue to optimising individual operations.