Comments (8)
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.
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.
These are easy enough to address.
@thelseraphim or @mitzimorris: if you have free cycles, this should be straightforward to tackle.
from stanc3.
Can this be closed?
from stanc3.
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.
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.
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.
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)
- [Build] Investigate Melange as an alternative to Js_of_ocaml
- [BUG] atan2 listed as vectorised in manual, vectorised sigs not in stanc3
- Stan compiler Error when using the normal_cdf function HOT 16
- [BUG] Reporting example where new compiler failed HOT 1
- [BUG] stanc3 v. 2.33.1: build error with OCaml 4.14.1 and JaneStreet 0.16.x: `Error: This expression has type [ `Use_Sys_unix ] This is not a function; it cannot be applied`; `Error: Unbound value Set.Poly.union` HOT 4
- Typechecker: Allow predicate-based typechecking of library functions HOT 3
- [BUG] SoA request is ignored with external c++ HOT 5
- Release support for building against ocaml 5 HOT 9
- [BUG] Github action doesn't build static binary for linux platform
- Improve message and handling of internal compiler errors
- [BUG] Array declaration no longer canonicalizes from deprecated format HOT 1
- Iinformative error message for expired array syntax
- CI: Switch from the ghr script to the github CLI
- Namespacing with external C++ and `--standalone-functions` causes issues HOT 4
- [BUG] Bogus parsing error with Stanc3 JS >= 2.34 - but only with QuickJS HOT 3
- #include support in stancjs HOT 1
- New stan error in R version 4.3.3 HOT 7
- Internal compiler error: TypeError: not a function HOT 2
- Expose built-in constraint transforms as _jacobian functions
- [FR] Add `lower_upper` constrain HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stanc3.