Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Real number arithmetic becomes really easy and considerably more accurate!</p> </blockquote> <p>No, it doesn't. The struct you describe only handles <em>rational</em> numbers, i.e. those that can be expressed as fractions. The set of real numbers includes both rational and irrational numbers. Most real-world calculations are done using real numbers, so you can't just limit yourself to the rationals and expect everything to be fine.</p> <blockquote> <p>I am wondering why no language I know does it.</p> </blockquote> <p>Most of the languages that I can think of make it possible to do exactly what you describe. In C, you can create a struct that contains numerator and denominator, and you can define a bunch of functions that operate on such structs. C++ makes things a LOT easier by letting you define a class and operations on that class -- same idea, much nicer syntax, etc. In fact, different sets of numbers are often used as examples in OO languages: you might start by defining a Rational class, and then extend that to include Imaginary numbers, and so on.</p> <p>I'd guess that the reason that there aren't more languages with built-in support for exact types probably has to do with the fact that processors don't directly support such operations. Modern processors include instructions that implement arithmetic operations for floating point types, so it's easy to include those in any language. Supporting exact types would mean building a math library into the language, and it's probably better on several levels to leave the math library out of the language and let those who need it build it into their software.</p> <p>If you're going to go to all the trouble to produce exact results, you probably don't want to limit yourself just to rationals, so the struct you give as an example isn't going to cut it. Being able to do exact calculations on rationals isn't very helpful if you fall back to inexact results the first time an irrational number shows up. Fortunately, there are sophisticated math systems out there. <a href="http://www.wolfram.com/mathematica/">Mathematica</a> is one well-known example.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload