Quotient *and* Remainder

342 views
Skip to first unread message

Anthony Ortiz

unread,
Apr 9, 2014, 1:01:57 PM4/9/14
to clo...@googlegroups.com
Hello everyone,

I am able to get the quotient or remainder by doing the following :

(quot 22 7)
-> 3

(rem 22 7)
-> 1

Is there a way to get back both the quotient and the remainder in one calculation rather than performing two separate calculations? And I don't mean writing a function that does both calculations behind the scenes and returning a list of both results.

Cheers!

Anthony

Sam Ritchie

unread,
Apr 9, 2014, 1:05:12 PM4/9/14
to clo...@googlegroups.com
So you DON'T mean:

(juxt quot rem)

?

April 9, 2014 11:01 AM
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
Sam Ritchie (@sritchie)

Ben Wolfson

unread,
Apr 9, 2014, 1:08:07 PM4/9/14
to clo...@googlegroups.com
Many languages expose a quotrem or divmod function that returns both at once, since they can both be calculated at once.
Ben Wolfson
"Human kind has used its intelligence to vary the flavour of drinks, which may be sweet, aromatic, fermented or spirit-based. ... Family and social life also offer numerous other occasions to consume drinks for pleasure." [Larousse, "Drink" entry]

Anthony Ortiz

unread,
Apr 9, 2014, 1:11:32 PM4/9/14
to clo...@googlegroups.com
No because that's simply calling what is essentially the same function twice. Internally the quotient and remainder functions are the same, the only difference is which part of the result they return. If I call the quotient function and then I call the remainder function then I am performing the same exact division twice and from a performance perspective I am wondering if there's a function that returns both values but only performs the division calculation once.

James Reeves

unread,
Apr 9, 2014, 1:46:37 PM4/9/14
to clo...@googlegroups.com
AFAIK, the JVM doesn't have a bytecode instruction for this, and therefore there's no mechanism to do this in Clojure except by using quot and rem separately.

- James


Anthony Ortiz

unread,
Apr 9, 2014, 2:05:56 PM4/9/14
to clo...@googlegroups.com, ja...@booleanknot.com
Ah yes of course... the same applies to the CLR. 
Reply all
Reply to author
Forward
0 new messages