Thread overview
Idiomatic D code to avoid or detect devision by zero
Jul 31, 2020
Andrea Fontana
July 31, 2020
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.)



July 31, 2020
On 7/31/20 9:55 AM, 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.)

c = b == 0 ? d : a/b;

I don't think a function would be shorter or clearer...

c = div(a, b, d);

Alternatively, you could use a type to effect the behavior you want.

-Steve
July 31, 2020
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
August 03, 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.
August 03, 2020
On Friday, 31 July 2020 at 14:18:15 UTC, Steven Schveighoffer wrote:
> On 7/31/20 9:55 AM, 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.)
>
> c = b == 0 ? d : a/b;
>
> I don't think a function would be shorter or clearer...
>
> c = div(a, b, d);
>
> Alternatively, you could use a type to effect the behavior you want.
>
> -Steve

Thanks, for the hints.
I find the ? :  - expressions sometimes hard to reed, especially when a and b are not so  simple expressions.

I prefer putting additional bracket around:
c = (b_expression == 0) ? (d_longer_expression) : (a_expression/b_expression);

???
August 03, 2020
On 8/3/20 5:53 AM, Martin Tschierschke wrote:
> On Friday, 31 July 2020 at 14:18:15 UTC, Steven Schveighoffer wrote:
>> On 7/31/20 9:55 AM, 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.)
>>
>> c = b == 0 ? d : a/b;
>>
>> I don't think a function would be shorter or clearer...
>>
>> c = div(a, b, d);
>>
>> Alternatively, you could use a type to effect the behavior you want.
>>
> 
> Thanks, for the hints.
> I find the ? :  - expressions sometimes hard to reed, especially when a and b are not so  simple expressions.
> 
> I prefer putting additional bracket around:
> c = (b_expression == 0) ? (d_longer_expression) : (a_expression/b_expression);

Yes, that is fine, and up to your preference. You may actually need the parentheses if the expressions somehow override the precedence of the ?: operator.

Even with symbol uses, I personally would do actually:

c = (b == 0 ? d : a/b);

Just because the ` = b == ` looks really bad to me.

-Steve
August 03, 2020
On Monday, 3 August 2020 at 14:50:36 UTC, Steven Schveighoffer wrote:
> On 8/3/20 5:53 AM, Martin Tschierschke wrote:
>> I prefer putting additional bracket around

For really long expressions you could also split it on multiple lines:

c = (b_expression == 0)
  ? (d_longer_expression)
  : (a_expression/b_expression);
August 06, 2020
On Monday, 3 August 2020 at 15:33:54 UTC, Dominikus Dittes Scherkl wrote:
[...]
> For really long expressions you could also split it on multiple lines:
>
> c = (b_expression == 0)
>   ? (d_longer_expression)
>   : (a_expression/b_expression);
+1 looks clean!