Idiomatic D code to avoid or detect devision by zero

Martin Tschierschke mt at smartdolphin.de
Mon Aug 3 09:45:17 UTC 2020


On Friday, 31 July 2020 at 15:19:25 UTC, Andrea Fontana wrote:
> On Friday, 31 July 2020 at 13:55:18 UTC, Martin Tschierschke 
> wrote:
>> What would be the idiomatic way to write a floating point 
>> division
>> occuring inside a loop and handle the case of division by zero.
>>
>> c = a/b; // b might be zero sometimes, than set c to an other 
>> value (d).
>>
>> (In the moment I check the divisor being zero or not, with an 
>> if-than-else structure,
>> but I find it ugly and so I ask here.)
>
> You should give a look at:
> https://dlang.org/phobos/std_experimental_checkedint.html
>
> You can try with checked!Throw and catch exceptions, for 
> example.
>
> Andrea

Thanks, I will look at it.


More information about the Digitalmars-d-learn mailing list