If your definition of a mathematical decimal number is 'one that when
written in decimal has a decimal point in it', do you want the answer
'true' or 'false' for the floating point number 1.0?  I don't see how that
definition of decimal number makes a lot of sense, or why you would want a
function that tells you that.

If you want a function that tells you 'can this number be represented
exactly as an integer?', then perhaps something like (== x
(convert-to-nearest-BigInteger x)), but I am not sure off hand a good way
to write convert-to-nearest-BigInteger.  According to one answer to this
StackOverflow question, converting a double or float to BigDecimal first,
then to the nearest BigInteger, may work:
http://stackoverflow.com/questions/17960186/is-there-anyway-to-convert-from-double-to-biginteger

Andy

On Mon, Jun 15, 2015 at 10:44 AM, John Gabriele <jmg3...@gmail.com> wrote:

> Thanks for the clarifications, all. Had not thought about exact vs.
> inexact results.
>
> Also, I see that --- if I simply want to know if a number is a
> (mathematical) decimal number, I can just do `(or (float? x) (decimal? x))`.
>
> -- John
>
>
>
>
> On Friday, June 12, 2015 at 12:08:56 PM UTC-4, Gary Verhaegen wrote:
>>
>> Your definitions are correct, but in a different domain: you are using
>> the mathematical definitions.
>>
>> What these functions give you is information about the representation of
>> the numbers in memory, not information about the numbers themselves. For
>> example, (integer? 1.0) will give you false.
>>
>> A rational number in mathematics is one that can be written as a fraction
>> (of integers), or equivalently one that does not have an infinite and
>> non-periodic expansion. A rational number in the sense of "rational?" in
>> Clojure is an object in memory which is actually stored as a couple of
>> integers, not merely a valie that could be stored that way.
>>
>> As others have mentioned, the way in which a number is stored will have
>> some impact on how much memory it takes up, how fast the computer can
>> compute operations on it, and how much precision will be lost with these
>> operations.
>>
>> As always, Clojure puts more emphasis on behaviours and interfaces than
>> on implementations, so you should really understand "rational?" as "will I
>> get an exact answer if I use operations that would give an exact answer
>> with a rational number (in the mathematical sense)?"
>>
>> For example, (/ 1M 3) will throw an exception rather than returning an
>> inexact, truncated answer, whereas (/ 1.0 3) will happily lie to you.
>> (According to the documentation, BigDecimal always returns a correct value
>> or throws an exception, except if you explicitly tell it to round. It can
>> represent values down to about 1e-2_147_483_647, and up to filling your
>> computer's memory.)
>>
>> For a first step towards understanding floating-point values, I would
>> recommend reading:
>>
>>
>> http://blog.reverberate.org/2014/09/what-every-computer-programmer-should.html
>>
>> On Friday, 12 June 2015, John Gabriele <jmg3...@gmail.com> wrote:
>>
>>> My understanding is that a rational number is one that can be written as
>>> a fraction. For example, 5.1, which can be written as 51/10. But Clojure
>>> seems to disagree:
>>>
>>> ~~~
>>> (rational? 51/10) ;=> true
>>> (rational? 5.1)   ;=> false (?!)
>>> ~~~
>>>
>>> Is my definition of "rational" incorrect?
>>>
>>> Also, my understanding is that a decimal number is one that has a
>>> decimal point in it, like, for example, 5.1. However:
>>>
>>> ~~~
>>> (decimal? 5.1) ;=> false (?!)
>>> ~~~
>>>
>>> And while typing this, I also notice that while `integer?` acts like I'd
>>> expect, `float?` does something weird:
>>>
>>> ~~~
>>> (integer? 5)   ;=> true     Yes
>>> (integer? 5N)  ;=> true     Yes
>>> (integer? 5.1) ;=> false
>>>
>>> (float? 5.1)  ;=> true
>>> (float? 5.1M) ;=> false (?!)
>>> ~~~
>>>
>>> Maybe I'm confusing "floating point number" with "decimal number" here?
>>> If so, what's the difference?
>>>
>>> Thanks!
>>>
>>>  --
>>> You received this message because you are subscribed to the Google
>>> Groups "Clojure" group.
>>> To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>  --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to