math.Mod in Go returns integer part instead of floating-point remainder

18,492

The result is correct. math.Mod returns the remainder, which really is 2 in this case. It's equivalent to the % operator, but for floating point numbers.

Share:
18,492
metaleap
Author by

metaleap

Freelance developer since high school (1999), increasingly into pure functional programming and the Lambda calculus Since 2015 --- Haskell, Elm, PureScript Since 2012 --- OpenGL+GLSL Since 2011 --- Go (aka golang), WebGL, Node.js, CoffeeScript Since 2008 --- F#, Python, Lisp/Clojure/Scheme Since 2006 --- SharePoint Since 2001 --- C#, ASP.NET, XSLT, XPath Since 2000 --- PHP, Java Since 1999 --- SQL, ASP, VisualBasic Since 1998 --- Pascal, Basic, HTML+CSS+JS Before long --- OCaml, Erlang Elixir, Scala, R, Rust, Nemerle..

Updated on June 22, 2022

Comments

  • metaleap
    metaleap almost 2 years

    Golang's math.Mod(10, 4) returns 2 -- ie. the integer part of division result 2.5 -- but shouldn't it be "the floating point remainder", that is, 0.5?