Click here to Skip to main content
15,895,799 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: C# Decimal Arithmetic Pin
trønderen12-Jan-21 15:09
trønderen12-Jan-21 15:09 
GeneralRe: C# Decimal Arithmetic Pin
Gerry Schmitz12-Jan-21 17:16
mveGerry Schmitz12-Jan-21 17:16 
GeneralRe: C# Decimal Arithmetic Pin
trønderen12-Jan-21 17:46
trønderen12-Jan-21 17:46 
GeneralRe: C# Decimal Arithmetic Pin
Harrison Pratt13-Jan-21 5:45
professionalHarrison Pratt13-Jan-21 5:45 
GeneralRe: C# Decimal Arithmetic Pin
obermd13-Jan-21 5:37
obermd13-Jan-21 5:37 
GeneralRe: C# Decimal Arithmetic Pin
trønderen13-Jan-21 8:19
trønderen13-Jan-21 8:19 
GeneralRe: C# Decimal Arithmetic Pin
Richard Deeming12-Jan-21 21:41
mveRichard Deeming12-Jan-21 21:41 
GeneralRe: C# Decimal Arithmetic Pin
trønderen13-Jan-21 2:40
trønderen13-Jan-21 2:40 
As the decimal representation does not provide any bit pattern for infinities, the divide operator has no way of returning any 'decimal.PositiveInfinity' - it doesn't exist. Similarly, there is no representation of Not a Number (NaN). Some values can be represented in float/double but not in decimal; others can be represented in decimal but not in float/double.

If you really want / need to handle infinities in decimal, you can define your own AugmentedDecimal struct that is a decimal plus the required flag bits for flagging the value as, say, positive / negative infinities or NaN. Then, a DivideByZeroException handler may set these flag bits appropriately. Of course you then have to define the operators for AugmentedDecimal to check the flag bits as appropriate before performing the operation.

This is built into the float hardware. decimal is essentially done in software, even without these augmentations. So it is slow anyway. But if that is what you need, that is the price you have to pay.

Then: My guess is that the tiny part of float/double handling software that handles infinities properly is a fraction of a percent. You don't get an exception here, but when you try to use the value as a normal number (because no exception has signaled to you the exceptional value), your program may do funny things. Tracing back to the division that caused the infinity may be non-trivial.

If you do write code that properly and throughout handles infinities (i.e. within that fraction of a percent), then I am sure that you would have no problems writing a proper handler for the decimal DivideByZeroException as well.
GeneralRe: C# Decimal Arithmetic Pin
Super Lloyd12-Jan-21 20:26
Super Lloyd12-Jan-21 20:26 
GeneralRe: C# Decimal Arithmetic Pin
Daniel Pfeffer12-Jan-21 20:56
professionalDaniel Pfeffer12-Jan-21 20:56 
GeneralRe: C# Decimal Arithmetic Pin
Kiriander12-Jan-21 22:01
Kiriander12-Jan-21 22:01 
GeneralRe: C# Decimal Arithmetic Pin
trønderen13-Jan-21 3:26
trønderen13-Jan-21 3:26 
GeneralRe: C# Decimal Arithmetic Pin
BernardIE531713-Jan-21 7:02
BernardIE531713-Jan-21 7:02 
GeneralRe: C# Decimal Arithmetic Pin
trønderen13-Jan-21 7:40
trønderen13-Jan-21 7:40 
GeneralRe: C# Decimal Arithmetic Pin
BotReject18-Jan-21 3:59
BotReject18-Jan-21 3:59 
GeneralBacon jam Pin
Mike Hankey12-Jan-21 9:25
mveMike Hankey12-Jan-21 9:25 
GeneralRe: Bacon jam Pin
Greg Utas12-Jan-21 9:35
professionalGreg Utas12-Jan-21 9:35 
GeneralRe: Bacon jam Pin
trønderen13-Jan-21 3:35
trønderen13-Jan-21 3:35 
GeneralGlory be! We're getting vaccinated! Pin
Cp-Coder12-Jan-21 6:21
Cp-Coder12-Jan-21 6:21 
GeneralRe: Glory be! We're getting vaccinated! Pin
Maximilien12-Jan-21 6:32
Maximilien12-Jan-21 6:32 
GeneralRe: Glory be! We're getting vaccinated! Pin
Cp-Coder12-Jan-21 6:34
Cp-Coder12-Jan-21 6:34 
GeneralRe: Glory be! We're getting vaccinated! Pin
Chris Maunder12-Jan-21 6:56
cofounderChris Maunder12-Jan-21 6:56 
GeneralRe: Glory be! We're getting vaccinated! Pin
Maximilien12-Jan-21 7:03
Maximilien12-Jan-21 7:03 
GeneralRe: Glory be! We're getting vaccinated! Pin
Gerry Schmitz12-Jan-21 8:32
mveGerry Schmitz12-Jan-21 8:32 
GeneralRe: Glory be! We're getting vaccinated! Pin
Chris Maunder12-Jan-21 10:54
cofounderChris Maunder12-Jan-21 10:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.