Code Monkey home page Code Monkey logo

Comments (8)

nhuurre avatar nhuurre commented on July 29, 2024 2

to_real is not necessary since you can always multiply by 1.0. And as far as I know division is the only place where the compiler doesn't autoconvert ints to reals.

The int division problem came up on the forum a few months ago. Does anyone object to my proposal for integer division operator?

int a;
int b;
int c = a %/ b; // integer division, rounds down
real d = a / b; // real division, no rounding

from stanc3.

bob-carpenter avatar bob-carpenter commented on July 29, 2024 1

Does anyone object to my proposal for integer division operator?

Yes, we can't change int / int to return a real because it breaks backward compatibility.

Multiplying by 1.0 will work, but it feels like a trick, so I'd prefer something like to_real for readability. It's more efficient to cast, but I can't see it ever being done enough to make a difference relative to everything else going on in a Stan program.

from stanc3.

syclik avatar syclik commented on July 29, 2024

These are easy enough to address.

@thelseraphim or @mitzimorris: if you have free cycles, this should be straightforward to tackle.

from stanc3.

VMatthijs avatar VMatthijs commented on July 29, 2024

Can this be closed?

from stanc3.

bob-carpenter avatar bob-carpenter commented on July 29, 2024

It hasn't been implemented yet. The suggestion is for a function

real div(real x, real y);

so that we can do:

real x = div(1, 2);

and get 0.5 rather than the 0 we get with 1 / 2. This is a minor issue.

from stanc3.

martinmodrak avatar martinmodrak commented on July 29, 2024

If integer division is added as a new operator, I think we should try to follow at least one other language and not invent our own, although most languages I've seen have named functions for integer (floor) division.

I've tried to do a survey of integer division operators in use:

  • // in Python (we can't have this as it is start of comment in Stan)
  • %/% in R - I like this because many Stan users use R (although few would be actually familiar with this operator)
  • \ used in Visual Basic as well as Access and other MS products. Also in ColdFusion.
  • รท used in Julia - although I think you can't write this on most keyboards...
  • div used in Pascal (this might cause some name conflicts)

Some languages define integer division also for floating point numbers. I think we shouldn't do this and only allow integer division of two ints.

I agree with @bob-carpenter that int / int needs to stay, but it should IMHO provide a warning with suggestion to either use to_real or integer division.

from stanc3.

nhuurre avatar nhuurre commented on July 29, 2024

int / int emits a warning right now and is going to round for at least a few releases. Users who want fractions have to use "the real division operator" *1.0/.
Changing the return type is breaking but often not silent; int division is mostly useful for calculating indices and there an unexpected real causes a type error.

The question is what to call the int division operator. @martinmodrak's survey has only one good option.

  • Python's // is my personal favorite but of course it's not available. (thanks, C++)
  • R's %/%: I'd prefer the shorter %/ but this is fine too.
  • Visual Basic \: Nope, \ is already a Stan operator, it means matrix left-division.
  • Julia รท: The parser does not allow non-ascii characters outside of comments.
  • Pascal div: A keyword that is an infix operator? Please no.

from stanc3.

bob-carpenter avatar bob-carpenter commented on July 29, 2024

So which is the good option? Wasn't @nhuurre's original proposal to overload ./ to scalar types? If we do that, it makes sense to cast to real since it's underlyingly a vector operation and vectors are all real. I don't think any of these (including just leaving things the way they are) make it clear to a reader of a program what's going on.

To undertand int / int is an int, they need to know that Stan works like C++ and Java, not like R. To understand what div or %/% or ./ means, they need to read our doc.

Also, we already use \ for left matrix division.

from stanc3.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.