开发者

Do any other languages have builtin rational types like scheme?

I haven't heard of any, most languages seem to just have division of ints round or be a floating point numb开发者_运维知识库er. Was it found to be a problem in scheme and so not used in other languages?


Are you asking about fractions? If so, Smalltalk has them:

(4/5) + (3/2) 

evaluates to:

(23/10)


Common Lisp:

CL-USER> (+ 4/5 3/2)
23/10

Factor:

( scratchpad ) 4/5 3/2 + .
23/10

Haskell 98:

Prelude> (4/5) + (3/2) :: Rational
23 % 10


You ask: "Was it found to be a problem in scheme and so not used in other languages?" The answer to that is "no", but it's an interesting question. Broadly speaking, Scheme/Racket is perhaps emblematic of a family of languages which, when given a choice between "correct" and "simple to implement", choose "correct" every time. The choice made by many other languages is to explicitly expose the representations of numbers as elements of a small finite set, and to require the programmer to operate in that sphere. Scheme/Racket instead provides a representation that can handle arbitrarily large numbers, limited only by the memory of the machine evaluating the code. This is not unlike the decision made by nearly all modern programming languages to use Garbage Collection, rather than forcing the programmer to explicitly allocate and deallocate memory.

As Chris points out, the representation of numbers as rationals almost always goes hand-in-hand with "bignums". There are a bunch of languages that support bignums--Scheme, Racket, Ruby, Python, etc.--and of course, any turing-complete language can be extended to handle bignums, including C.


Scheme's rationals have bignums backing them. Most languages don't have built-in bignums.

In my opinion, it's pointless to have built-in rationals without built-in bignums, because without bignums, you start to lose precision after a certain point, and you may as well be upfront about the lossiness by using floating-point.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜