digitalmars.D.learn - Idiomatic D code to avoid or detect devision by zero
- Martin Tschierschke (7/7) Jul 31 2020 What would be the idiomatic way to write a floating point division
- Steven Schveighoffer (6/14) Jul 31 2020 c = b == 0 ? d : a/b;
- Martin Tschierschke (9/26) Aug 03 2020 Thanks, for the hints.
- Steven Schveighoffer (8/35) Aug 03 2020 Yes, that is fine, and up to your preference. You may actually need the
- Dominikus Dittes Scherkl (7/9) Aug 03 2020 For really long expressions you could also split it on multiple
- Martin Tschierschke (4/9) Aug 06 2020 On Monday, 3 August 2020 at 15:33:54 UTC, Dominikus Dittes
- Andrea Fontana (6/14) Jul 31 2020 You should give a look at:
- Martin Tschierschke (2/19) Aug 03 2020 Thanks, I will look at it.
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.)
Jul 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
Jul 31 2020
On Friday, 31 July 2020 at 14:18:15 UTC, Steven Schveighoffer wrote:On 7/31/20 9:55 AM, Martin Tschierschke wrote: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); ???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
Aug 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: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. -SteveOn 7/31/20 9:55 AM, Martin Tschierschke wrote: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);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.
Aug 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:For really long expressions you could also split it on multiple lines: c = (b_expression == 0) ? (d_longer_expression) : (a_expression/b_expression);I prefer putting additional bracket around
Aug 03 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!
Aug 06 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
Jul 31 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:Thanks, I will look at it.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
Aug 03 2020