JavaScript, the language, doesn’t (AFAIK; I don’t keep track of this rapidly moving target) have integers (but does have integer arrays, nowadays. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Type...)
If the type were integer,
let x = 2
let y = x / 8
would have y = 0, but its value will be 0.25.I'd bat both eyelids, that's the kind of surprising design decision that would turn me away from a language. To my knowledge, no major programming language does this, and there's wisdom in the principle of least surprise.
edit: I am of course mistaken, Python does exactly this. Corrected by chrisseaton again!
Lol prepare to bat your eyelids.
% python
Python 3.8.5 (default, Jul 21 2020, 10:48:26)
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 1 / 2
0.5If you're up for a game of No True Scotsman, can you name me a statically typed language that does the same?
https://docs.microsoft.com/en-us/dotnet/visual-basic/languag...
If you don't believe me, perhaps you'll believe output from the machine.
https://tio.run/##K0vSTc4vSv3/3zc/pTQnVcEjNScnn4szuDRJwTcxM0...
* edited for guideline compliance
> Did you read it?
The HN guidelines ask that you don't do that.
I don't think it settles anything, but a trivial function that divides two integers and returns the result compiles to this:
IL_0000: ldarg.1
IL_0001: conv.r8
IL_0002: ldarg.2
IL_0003: conv.r8
IL_0004: div
IL_0005: ret
conv.r8 is an explicit conversion to double precision float in the target language, MSIL.[0] https://nim-lang.org/docs/manual.html#types-preminusdefined-...
```
GHCi, version 8.6.5: http://www.haskell.org/ghc/ :? for help
Prelude> 1 / 2
0.5
```
This is because 1 and 2 are polymorphic and get instantiated to floats here. Witchcraft.
Note: there are several potential instances:
instance RealFloat a => Fractional (Complex a)
-- Defined in ‘Data.Complex’
instance HasResolution a => Fractional (Data.Fixed.Fixed a)
-- Defined in ‘Data.Fixed’
instance Integral a => Fractional (Ratio a)
-- Defined in ‘GHC.Real’
...plus two others GHCI> :i (/)
class Num a => Fractional a where
(/) :: a -> a -> a
...
-- Defined in ‘GHC.Real’
infixl 7 /
The key is that integer types aren't Fractional, so the Haskell 2010 defaulting rules[1] cause the literals to be interpreted as Double. With an appropriate default declaration GHC could have picked Ratio Integer, Complex Double, etc. instead, but without one Integer and Double are the only options and Integer wouldn't satisfy the constraints.[1] https://www.haskell.org/onlinereport/haskell2010/haskellch4....
https://docs.julialang.org/en/v1/manual/integers-and-floatin...