News for hackers. Stuff that matters.
Re:C-style: My favorite C programming practices by a_e_k on Monday May 20, 2024 @02:17AM (#40412812)
I feel like I probably agree with about 80% of this. It also seems like this would apply fairly well to C++ as well.

One thing that I'll strongly quibble with: "Use double rather than float, unless you have a specific reason otherwise".

As a graphics programmer, I've found that single precision will do just fine in the vast majority of cases. I've also found that it's often better to try to make my code work well in the single precision while keeping an eye out for precision loss. Then I can either rewrite my math to try to avoid the precision loss, or selectively use double precision just in the parts where its needed. I think that using double precision from the start is a big hammer that's often unneeded. And using single precision buys you double the number of floats moving through your cache and memory bandwidth compared to using double precision.

Re:C-style: My favorite C programming practices by ack_complete on Monday May 20, 2024 @03:37AM (#40413192)
I'm torn both ways on the double issue. On the one hand, doubles are much more widely supported these days, and will save you from some common scenarios. Timestamps are a particular one, where a float will often degrade on a time scale that you care about, and doubles not. A double will also hold any int value without loss (on mainstream platforms), and has enough precision to allow staying in world coordinates for 3D geometry without introducing depth buffer problems.

OTOH, double precision is often just a panacea. If you don't know the precision requirements of your algorithm, how do you know that double precision will work either? Some types of errors will compound without anti-drifting protection in ways that are exponential, where the extra mantissa bits from a double will only get you a constant factor of additional time.

There are also current platforms where double will land you in very significant performance problems, not just a minor hit. GPUs are a particularly fun one -- there are currently popular GPUs where double precision math runs at 1/32 the rate of single precision.

Re:C-style: My favorite C programming practices by teddyh on Monday May 20, 2024 @06:51AM (#40414132)
I think you used the word “panacea” incorrectly. Judging by context, I would guess that the word “band-aid” would better convey your intended meaning.
Re:C-style: My favorite C programming practices by nine_k on Monday May 20, 2024 @07:43AM (#40414420)
A "panacea" is something that cures.every illness. 64-bit floats could do just that, in the cases listed. The cost of it may be higher than one cares to pay though.

And when the cure fails to be adequate, well, it becomes a band-aid, a temporary measure in search of a real solution.

Re:C-style: My favorite C programming practices by zbentley on Monday May 20, 2024 @08:40AM (#40414822)
Perhaps "placebo" was intended?
Re:C-style: My favorite C programming practices by a1369209993 on Monday May 20, 2024 @02:08PM (#40418261)
IIUC, they're using the word itself correctly, but they mean "double precision is often used with the intent of it being a panacea".
Re:C-style: My favorite C programming practices by orthoxerox on Monday May 20, 2024 @08:02AM (#40414519)
Are there C++ libs that use floating points for timestamps? I was under the impression that most stacks have accepted int64 epoch microseconds as the most reasonable format.
Re:C-style: My favorite C programming practices by gpderetta on Monday May 20, 2024 @08:37AM (#40414803)
> int64 epoch microseconds

surely nanoseconds is the truth.

Re:C-style: My favorite C programming practices by orthoxerox on Monday May 20, 2024 @09:17AM (#40415131)
LONG_MAX nanoseconds is just Friday, April 11, 2262 11:47:16.854 PM, not exactly a future-proof approach. I guess having Tuesday, September 21, 1677 12:12:43.145 AM as the earliest expressible timestamp neatly sidesteps the problem of proleptic Gregorian vs Julian calendars.
Re:C-style: My favorite C programming practices by Const-me on Monday May 20, 2024 @09:30AM (#40415262)
I think Apple is still doing that: https://developer.apple.com/documentation/foundation/nstimei...

Couple decades ago Microsoft did that too, VT_DATE in old OLE Automation keeping FP64 value inside. Luckily, their newer APIs and frameworks are using uint64 with 100-nanoseconds ticks.

Re:C-style: My favorite C programming practices by ack_complete on Monday May 20, 2024 @12:13PM (#40417040)
Don't have a publicly visible reference to give at the moment, but it's still sometimes seen where relative timestamps are being tracked, such as in an audio library tracking time elapsed since start. It's probably less used for absolute time where the precision problems are more obvious.
Re:C-style: My favorite C programming practices by e4m2 on Monday May 20, 2024 @12:45PM (#40417362)
It's very common in games.

Integers are always an option, of course, but in this context it's hard to beat the convenience of just storing seconds in a floating point number.

Related: https://randomascii.wordpress.com/2012/02/13/dont-store-that...

Re:C-style: My favorite C programming practices by lifthrasiir on Monday May 20, 2024 @03:15AM (#40413101)
I think the fact that graphics care a lot more about efficiency over marginal accuracy qualifies for a specific reason. Besides from that and a few select areas like ML, almost any reason to use `float` by default vanishes.
Re:C-style: My favorite C programming practices by amszmidt on Monday May 20, 2024 @02:28AM (#40412874)
The one about not using 'switch' and instead using combined logical comparisons is terrible ... quite opinionated, but that is usually the case with these type of style guides.
Re:C-style: My favorite C programming practices by aulin on Monday May 20, 2024 @03:09AM (#40413068)
it's like they purposely add some controversial rule just for engagement
Re:C-style: My favorite C programming practices by ezconnect on Monday May 20, 2024 @03:28AM (#40413165)
He even uses 'switch' on his code.
Re:C-style: My favorite C programming practices by mcinglis on Monday May 20, 2024 @11:26AM (#40416578)
As the author 10 years later, I agree. A hard-and-fast rule to ban switch, as that rule seems to advocate, is silly and terrible.

Switch has many valid uses. However, I also often see switch used in places where functional decomposition would've been much better (maintainable / testable / extensible). So I think there's still value in advocating for those switch alternatives, such as that rule's text covers. Not that I agree with everything there either. But, useful for discussion!

Re:C-style: My favorite C programming practices by sdk77 on Monday May 20, 2024 @06:39AM (#40414081)
There are popular embedded platforms like STM32 that don't have hardware double support, but do have hardware float support. Using double will cause software double support to be linked and slow down your firmware significantly.
Re:C-style: My favorite C programming practices by AnimalMuppet on Monday May 20, 2024 @08:46AM (#40414880)
OK, but if you're writing for that kind of platform, you know it. Don't use double there? Sure. "Don't use double on non-embedded code just because such platforms exist" doesn't make sense to me.

Sure, my code could maybe run on an embedded platform someday. But the person importing it probably has an editor that can do a search and replace...

Re:C-style: My favorite C programming practices by JKCalhoun on Monday May 20, 2024 @08:08AM (#40414568)
> I feel like I probably agree with about 80% of this.

What I was thinking too. There's something in here to offend everyone, and that's probably a good thing.

Re:C-style: My favorite C programming practices by projektfu on Monday May 20, 2024 @10:49AM (#40416136)
The issue for me is that unlabeled constants are doubles and they can cause promotion where you don't expect it, leading to double arithmetic and rounding instead of single arithmetic. Minor issue, but hidden behavior.
Re:C-style: My favorite C programming practices by atiedebee on Monday May 20, 2024 @04:39PM (#40420065)
What's even more annoying is that the *printf functions take in double which forces you to cast all of the floats you pass in when using -Wdouble-promotion
Re:C-style: My favorite C programming practices by Lvl999Noob on Monday May 20, 2024 @12:27PM (#40417178)
I think your case comes under the "specific reason to use `float`"? If I am writing some code and I need floating point numbers, then without any more context, I will choose `double`. If I have context and the context makes it so `float`s are vastly better, then I will use `float`s.
Re:C-style: My favorite C programming practices by sixthDot on Monday May 20, 2024 @09:29PM (#40423014)
funnily the example used, i.e `printf` of single values is very special. Under the hood, variadic arguments that are `single` are actually converted to `double`. See the `cvtss2sd` in [1].

[1]: https://godbolt.org/z/Yr7Kn4vqr

Re:C-style: My favorite C programming practices by spookie on Monday May 20, 2024 @11:41PM (#40423842)
Yeah, sometimes as a graphics programmer you don't even want the precision provided by built-in functions! As it has been pointed out though, be careful about error propagation