4 ms·
That's perfectly reasonable; there's nothing to say that `/` has type `int -> int -> int` even in a strongly-typed language. I wouldn't bat an eyelid to discove
by Smaug123 6y ago
That's perfectly reasonable; there's nothing to say that `/` has type `int -> int -> int` even in a strongly-typed language. I wouldn't bat an eyelid to discover that `/` had type `int -> int -> float` in any language. The problem is when you discover that `+` does not have type `int -> int -> int` (e.g. when you try and increment a very large integer and floating point stops being able to represent it).
- MaxBarraclough 6y ago> I wouldn't bat an eyelid to discover that `/` had type `int -> int -> float` 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!
- chrisseaton 6y ago> 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. 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.5
- MaxBarraclough 6y agoI'll bat my eyelids, but only after I kick myself ;-P If you're up for a game of No True Scotsman, can you name me a statically typed language that does the same?
- recursive 6y agoVisual basic
- MaxBarraclough 6y agoI don't think so. According to this, in Visual Basic, the '/' operator isn't defined for integer types. https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/data-types-of-operator-results#floating-point-arithmetic https://docs.microsoft.com/en-us/dotnet/visual-basic/languag...
- recursive 6y agoIntegers are right there in the table on the page you're linking to. If you don't believe me, perhaps you'll believe output from the machine. https://tio.run/##K0vSTc4vSv3/3zc/pTQnVcEjNScnn4szuDRJwTcxM09Dk4uTMyUzVyFPIbFYITOvJDU9tUjBVsHQACqegipuAhR2zs8rzs9J1QsvyixJ9cnMS9UwNFDQVzABGuWal6IANJoLREMs/P8fAA https://tio.run/##K0vSTc4vSv3/3zc/pTQnVcEjNScnn4szuDRJwTcxM0... * edited for guideline compliance
- MaxBarraclough 6y agoLooks like you're right. In my defense the page I linked states that The / operator is defined only for the Decimal, Single, and Double data types. I suppose it accepts integer types by means of 'widening'? > Did you read it? The HN guidelines ask that you don't do that.
- recursive 6y agoI've edited my comment for guideline compliance. 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.
- 1wd 6y agoNim https://play.nim-lang.org/#ix=2zbk https://play.nim-lang.org/#ix=2zbk
- MaxBarraclough 6y agoInteresting, thanks. I see Nim uses '/%' for integer division. [0] I can't say I like that decision. [0] https://nim-lang.org/docs/manual.html#types-preminusdefined-integer-types https://nim-lang.org/docs/manual.html#types-preminusdefined-...
- dom96 6y agoUnsigned integer division. In practice I haven’t seen this operator used anywhere yet.
- Smaug123 6y agoHave I gone mad? (EDIT: it appears I have.) ``` GHCi, version 8.6.5: http://www.haskell.org/ghc/ 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.
- MaxBarraclough 6y agoI think you're still right though. I know Haskell has an unusual approach to numbers, but it looks like the resultant type is indeed different from the type of the operands. I got this from typeOf (/): 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
- nybble41 6y agoThere are many instances for Fractional, but the resultant type of the (/) operator is always the same as the operands: 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.html#x10-750004.3 https://www.haskell.org/onlinereport/haskell2010/haskellch4....
- e12e 6y agoJulia? There's an integer division operator, but the default is... More complex: https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/ https://docs.julialang.org/en/v1/manual/integers-and-floatin...
- andi999 6y agoPython2 did it the other way, wasnt it?
- cyberbanjo 6y ago(/ 1 2) #=> 1/2 Clojure as well, though it's a rational not a float.
- MaxBarraclough 6y agoInteresting, thanks