Best search algorithm to find condition within a range

Marko Rauhamaa marko at pacujo.net
Thu Apr 9 20:11:03 CEST 2015


Steven D'Aprano <steve+comp.lang.python at pearwood.info>:

> There are good ways to deal with out-of-range results in systems
> languages:
>
> - well-defined wrap-around behaviour;
> - saturation;
> - trapping.

Some months back I reported a bug in LXDE's CPU monitor applet: after a
couple of months of operation, the CPU monitor displayed a 100% CPU
utilization rate on an idle machine (on a 32-bit machine).

The reason was that scanf("%ld") saturated at LONG_MAX instead of
wrapping gracefully. Wrapping behavior would have resulted in a
perfectly well-behaved difference (and CPU utilization). As it stood,
saturation destroyed the sane-looking calculation.

The solution was to move to scanf("%lld") and prevent satufation. Again,
I don't blame the code; I blame the spec. Writing a wrapping
implementation of scanf("%ld") is the simplest possible, most obvious
and most obviously correct approach. Only it wouldn't be
standards-compliant.


Marko



More information about the Python-list mailing list