This content originally appeared on Level Up Coding - Medium and was authored by Yogesh Parwani
Why dividing(/) by 0 does not throw an error in Dart — How Dart handles numbers
How does Dart handle numbers and operations while targetting so many platforms?
Table of Contents
Dart targets multiple platforms, and numbers in dart behave differently depending on the underlying platform.
Hierarchy
Numbers, in programming, are usually divided into two types: integer values and fractional values. In dart, int represents integers, and double represents fractional values.
num is the parent class for int and double.
When in doubt regarding the value being int or double, use num instead of dynamic, which handles both the scenarios.
Platforms
Dart targets multiple platforms but for ease let’s divide it into two types: Native and Web.
Native
It usually includes 64-bit processors like mobile and desktops. int maps to a 64-bit integer representation and double maps to a 64-bit IEEE floating-point representation that matches the underlying processor.
On native, a number can either be an int or double. It can’t be both.
Web
As the name suggests — it includes web where javascript is the primary execution engine. Dart code compiles to javascript, and there is a single numeric representation: a 64-bit double-precision floating-point value. So, Dart maps both int and double to this representation.
On the web, a number can be both int and double.
So, if there is only a single representation, how do we represent int on the web? On the web, int is defined as a double value without the fractional part(if it is zero).
Alright! Let’s jump to the exciting part, Snippets!
On evaluation, line no. 2 returns true on both native and web which is expected.
Line no. 3 returns false and true on native and web, respectively. As we have covered, It maps int to integers and double to a floating point on native, but in the case of the web, it only maps to a floating point.
Line no. 5, on native, is false as it is mapped to a double representation but true on the web. Line 6, true on both native and web.
Line no. 8 evaluates 1.0 and 1 on native and web, respectively. On the web, the zero-valued fractional is removed, hence 1.
Line no.10 evaluates to same irrespective of the platform.
Division Operator (/)
Implementation: double operator /(num other);
The division operator has a return double. The catch is it performs a double division. Because of this, it does not throw an error when divided by zero. Okay, what does it return then?
When dividing a number by zero, Dart returns double.infinity, a constant with 1.0/0.0 as its implementation.
But what if we don’t want double division? What if we want to do integer division? No worries, Dart has got us covered.
Truncating Division Operator (~/)
Implementation: int operator ~/(num other);
It has a return type of int and performs integer division. Both the operand must be an integer and non zero. If either operand is double, the other operand is also converted into double and truncated later.
Final Thoughts
Dart is smart enough to handle this across the platforms, so we don’t need to take it into our day-to-day tasks. Nonetheless, if we have assertions or checks that compare String, we have to handle it depending on the platform. For instance,
The above evaluates to true and false on Native and web, respectively, as the trailing zeros are removed on the web.
Awesome! Pat yourself on the back for reaching till the end. I hope I added some value to the time you invested. Find out more examples on the GitHub repository, and reach out on Twitter or LinkedIn for suggestions/Questions or any topic you’d like me to cover. You can support it by clapping👏, and thanks for reading :) Follow for more😄
Until next time, folks!
Why dividing(/) by 0 does not throw an error in Dart — How Dart handles numbers was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Yogesh Parwani
Yogesh Parwani | Sciencx (2022-05-03T11:43:57+00:00) Why dividing(/) by 0 does not throw an error in Dart — How Dart handles numbers. Retrieved from https://www.scien.cx/2022/05/03/why-dividing-by-0-does-not-throw-an-error-in-dart-how-dart-handles-numbers/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.