Math is hard
-
Just found a line of code which shows enormous mathematical competence:
_Timer.Interval = 1000 * (1.0 / _VideoFileReader.FrameRate.Value);
Well, FrameRate.Value is a double giving the number of frames per second. Timer.Interval is a double which indicates the interval in milliseconds.
So, what about:
_Timer.Interval = 1000 / _VideoFileReader.FrameRate.Value;
-
@BernieTheBernie as a C++ guy, I'm more worried about identifiers starting with underscore and uppercase.
-
@BernieTheBernie said in Math is hard:
Just found a line of code which shows enormous mathematical competence:
_Timer.Interval = 1000 * (1.0 / _VideoFileReader.FrameRate.Value);
Well, FrameRate.Value is a double giving the number of frames per second. Timer.Interval is a double which indicates the interval in milliseconds.
So, what about:
_Timer.Interval = 1000 / _VideoFileReader.FrameRate.Value;
Given that any rounding/floating-point multiple operation loss is insignificant here, the first is more clear about what is happening and imo should be preferred if that condition holds. Although a short comment would achieve the same effect with potentially less loss.
-
@Dreikin No. Multiplying by the reciprocal is not more clear than just dividing. I do not care what the processor thinks on this subject.
-
@Gribnit well, actually, processor agrees with you on that.
-
In Go, this would be
time.Second / time.Duration(framesPerSecond)
. Assuming this is C#, would usingTimeSpan
to do something similar be possible?
-
@ben_lubar said in Math is hard:
In Go, this would be time.Second / time.Duration(framesPerSecond).
Assuming the author would care about proper typing. Guessing from how they write C#, they wouldn't.
-
@Gribnit said in Math is hard:
@Dreikin No. Multiplying by the reciprocal is not more clear than just dividing. I do not care what the processor thinks on this subject.
The CPU doesn't even see the difference if the compiler isn't stuck in the 80s. I find the first version marginally clearer, although a brief comment like "frame duration in ms" would have made both a lot more obvious. If you don't know
_Timer.Interval
is ms you'd start off wondering why they'd calculate the duration of 1000 frames.