unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* Why 'inexact' and 'exact' doesn't check 'number?' first?
@ 2012-12-12  3:21 Nala Ginrut
  2012-12-12  4:39 ` Daniel Hartwig
  2013-01-21 20:52 ` Andy Wingo
  0 siblings, 2 replies; 7+ messages in thread
From: Nala Ginrut @ 2012-12-12  3:21 UTC (permalink / raw)
  To: guile-devel

It's weird to see that:
(exact? 'a) 
================err msg===============
ERROR: In procedure exact?:
ERROR: In procedure exact?: Wrong type argument in position 1: a
==================end=================

And I have to do this:
(define (fraction? obj)
  (and (number? obj) (inexact? obj)))


Why not 'exact?' and 'inexact?' doesn't check 'number?' first? 




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Why 'inexact' and 'exact' doesn't check 'number?' first?
  2012-12-12  3:21 Why 'inexact' and 'exact' doesn't check 'number?' first? Nala Ginrut
@ 2012-12-12  4:39 ` Daniel Hartwig
  2012-12-12  5:55   ` Nala Ginrut
  2013-01-21 20:52 ` Andy Wingo
  1 sibling, 1 reply; 7+ messages in thread
From: Daniel Hartwig @ 2012-12-12  4:39 UTC (permalink / raw)
  To: Nala Ginrut; +Cc: guile-devel

On 12 December 2012 11:21, Nala Ginrut <nalaginrut@gmail.com> wrote:
> It's weird to see that:
> (exact? 'a)
> ================err msg===============
> ERROR: In procedure exact?:
> ERROR: In procedure exact?: Wrong type argument in position 1: a
> ==================end=================
>
> And I have to do this:
> (define (fraction? obj)
>   (and (number? obj) (inexact? obj)))

“Fraction” is not a very precise term or relevent to the type
hierarchy in Scheme, please do not use that.  Disregard that GOOPS
define such a class, the more precise term is “rational” for which
there is already a predicate.

Your fraction? predicate is not equivalent to the GOOPS <fraction>
class, which are exact? and (in all cases I believe) also rational?.

>
>
> Why not 'exact?' and 'inexact?' doesn't check 'number?' first?

Performance. They are intended to be used in numerical paths and
requiring them to check first number? every time is very inefficient.
You will note that they are documented as “exact? Z“ rather than
“exact? obj”.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Why 'inexact' and 'exact' doesn't check 'number?' first?
  2012-12-12  4:39 ` Daniel Hartwig
@ 2012-12-12  5:55   ` Nala Ginrut
  2012-12-12  6:07     ` Daniel Hartwig
  0 siblings, 1 reply; 7+ messages in thread
From: Nala Ginrut @ 2012-12-12  5:55 UTC (permalink / raw)
  To: Daniel Hartwig; +Cc: guile-devel

On Wed, 2012-12-12 at 12:39 +0800, Daniel Hartwig wrote:
> On 12 December 2012 11:21, Nala Ginrut <nalaginrut@gmail.com> wrote:
> > It's weird to see that:
> > (exact? 'a)
> > ================err msg===============
> > ERROR: In procedure exact?:
> > ERROR: In procedure exact?: Wrong type argument in position 1: a
> > ==================end=================
> >
> > And I have to do this:
> > (define (fraction? obj)
> >   (and (number? obj) (inexact? obj)))
> 
> “Fraction” is not a very precise term or relevent to the type
> hierarchy in Scheme, please do not use that.  Disregard that GOOPS
> define such a class, the more precise term is “rational” for which
> there is already a predicate.

Many thanks!
But rational? returns TRUE both 1.5 & 1/2, I need a predicate to check
between 'float' and 'fraction' distinctly.

Are you suggesting I use (is-a? obj <fraction>) for 'fraction?' ?



> 
> Your fraction? predicate is not equivalent to the GOOPS <fraction>
> class, which are exact? and (in all cases I believe) also rational?.
> 
> >
> >
> > Why not 'exact?' and 'inexact?' doesn't check 'number?' first?
> 
> Performance. They are intended to be used in numerical paths and
> requiring them to check first number? every time is very inefficient.
> You will note that they are documented as “exact? Z“ rather than
> “exact? obj”.






^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Why 'inexact' and 'exact' doesn't check 'number?' first?
  2012-12-12  5:55   ` Nala Ginrut
@ 2012-12-12  6:07     ` Daniel Hartwig
  2012-12-12  6:32       ` Mark H Weaver
  0 siblings, 1 reply; 7+ messages in thread
From: Daniel Hartwig @ 2012-12-12  6:07 UTC (permalink / raw)
  To: Nala Ginrut; +Cc: guile-devel

On 12 December 2012 13:55, Nala Ginrut <nalaginrut@gmail.com> wrote:
> Are you suggesting I use (is-a? obj <fraction>) for 'fraction?' ?

Absolutely not.  Use inexact? if you wish to determine that the
*storage* of a value is using floating point format.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Why 'inexact' and 'exact' doesn't check 'number?' first?
  2012-12-12  6:07     ` Daniel Hartwig
@ 2012-12-12  6:32       ` Mark H Weaver
  2012-12-12  6:39         ` Daniel Hartwig
  0 siblings, 1 reply; 7+ messages in thread
From: Mark H Weaver @ 2012-12-12  6:32 UTC (permalink / raw)
  To: Daniel Hartwig; +Cc: guile-devel

Daniel Hartwig <mandyke@gmail.com> writes:
> On 12 December 2012 13:55, Nala Ginrut <nalaginrut@gmail.com> wrote:
>> Are you suggesting I use (is-a? obj <fraction>) for 'fraction?' ?
>
> Absolutely not.  Use inexact? if you wish to determine that the
> *storage* of a value is using floating point format.

Apologies in advance for being pedantic, but there is no guarantee that
inexact numbers are represented in floating point format.  Having said
that, I'm not aware of any current Scheme implementation that uses a
different representation for inexacts.

Nala Ginrut <nalaginrut@gmail.com> writes:
> (define (fraction? obj)
>   (and (number? obj) (inexact? obj)))

This definition implies that 3.1415927 is a fraction, and 1/2 is not.
I suspect you want something closer to this:

  (define (fraction? obj)
    (and (rational? obj)
         (exact? obj)
         (not (integer? obj))))

     Regards,
       Mark



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Why 'inexact' and 'exact' doesn't check 'number?' first?
  2012-12-12  6:32       ` Mark H Weaver
@ 2012-12-12  6:39         ` Daniel Hartwig
  0 siblings, 0 replies; 7+ messages in thread
From: Daniel Hartwig @ 2012-12-12  6:39 UTC (permalink / raw)
  To: guile-devel

On 12 December 2012 14:32, Mark H Weaver <mhw@netris.org> wrote:
> Daniel Hartwig <mandyke@gmail.com> writes:
>> On 12 December 2012 13:55, Nala Ginrut <nalaginrut@gmail.com> wrote:
>>> Are you suggesting I use (is-a? obj <fraction>) for 'fraction?' ?
>>
>> Absolutely not.  Use inexact? if you wish to determine that the
>> *storage* of a value is using floating point format.
>
> Apologies in advance for being pedantic, but there is no guarantee that
> inexact numbers are represented in floating point format.  Having said
> that, I'm not aware of any current Scheme implementation that uses a
> different representation for inexacts.

Quite right.

Pedantism is appropriate given the context of writing a Scheme
expression printer.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Why 'inexact' and 'exact' doesn't check 'number?' first?
  2012-12-12  3:21 Why 'inexact' and 'exact' doesn't check 'number?' first? Nala Ginrut
  2012-12-12  4:39 ` Daniel Hartwig
@ 2013-01-21 20:52 ` Andy Wingo
  1 sibling, 0 replies; 7+ messages in thread
From: Andy Wingo @ 2013-01-21 20:52 UTC (permalink / raw)
  To: Nala Ginrut; +Cc: guile-devel

On Wed 12 Dec 2012 04:21, Nala Ginrut <nalaginrut@gmail.com> writes:

> It's weird to see that:
> (exact? 'a) 
> ================err msg===============
> ERROR: In procedure exact?:
> ERROR: In procedure exact?: Wrong type argument in position 1: a
> ==================end=================

scheme@(guile-user)> (positive? "foo")
ERROR: In procedure positive?:
ERROR: In procedure positive?: Wrong type argument in position 1: "foo"

Same reason; exact? operates on numbers.

Cheers,

Andy
-- 
http://wingolog.org/



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2013-01-21 20:52 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-12-12  3:21 Why 'inexact' and 'exact' doesn't check 'number?' first? Nala Ginrut
2012-12-12  4:39 ` Daniel Hartwig
2012-12-12  5:55   ` Nala Ginrut
2012-12-12  6:07     ` Daniel Hartwig
2012-12-12  6:32       ` Mark H Weaver
2012-12-12  6:39         ` Daniel Hartwig
2013-01-21 20:52 ` Andy Wingo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).