When communicating knowledge via source code is as complicated as mathematics, then the dream of using programming languages for knowledge transfer is out and over.
Currently I am trying to figure out how I can generate a sine-wave sound on my computer's audio, using Java 21 (openjdk) on Ubuntu LINUX 24.04. I came over some stackoverflow examples that try to communicate how you could do that. I collected three of these snippets and tried them all out. It turned out that two of them did not work properly when given more complex data. When I searched for their bugs, I came across that old problem with arithmetic operator precedence.
The precedence that we learnt is that multiplications and divisions (*, /) are performed before additions and subtractions (+, -). So there is a rule for two groups of arithmetic operators, but not for every operator individually. Is it safe to mix multiplications and divisions in any order?
public class Main { public static void main(String[] args) { int wrong = 100 / 8 * 2; int right = 100 * 2 / 8; System.out.println("wrong = "+wrong+", right = "+right); } // output: wrong = 24, right = 25 }
As this example shows, it is not safe to mix multiplications and divisions in any order. But does that apply to integer operations only (because it drops the comma-part of the number), or does this affect floating point operations too?
public static void main(String[] args) { double one = 100.743 / 8.573 * 2.159; double two = 100.743 * 2.159 / 8.573; System.out.println("one = "+one+", two = "+two); } // output: one = 25.37083133092266, two = 25.37083133092266
Looks like floating point operations can be written in any order. A subsequent question is: how do integers and doubles do when mixed in one operation?
public static void main(String[] args) { double one = 100 / 8 * 2.159; double two = 100 * 2.159 / 8; System.out.println("one = " + one + ", two = " + two); } // output: one = 25.907999999999998, two = 26.987499999999997
This example shows that it is not safe to mix integers and doubles in any order. So take care when writing such source code. Always cast every integer to a double when mixed with floating point numbers.
public static void main(String[] args) { double one = (double) 100 / (double) 8 * 2.159; double two = (double) 100 * 2.159 / (double) 8; System.out.println("one = " + one + ", two = " + two); } // one = 26.987499999999997, two = 26.987499999999997
In case you use some artificial intelligence chat-application to write your source code, be aware that this does not create source code on its own, it presents you code from sites like stackoverflow. That doesn't mean stackoverflow is evil, it is really very helpful, but you have to be a critical consumer of those snippets and mostly have to fix and refactor them heavily.
By the way, the sine-wave generator snippets do not have such an arithmetic calculation bug. I am still searching for the reason, maybe I will write another Blog about it.
Keine Kommentare:
Kommentar veröffentlichen