caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Finding the sign of a float
@ 2003-02-01  6:35 Shawn Wagner
  2003-02-01  9:05 ` Chris Hecker
  2003-02-01 12:56 ` John Carr
  0 siblings, 2 replies; 5+ messages in thread
From: Shawn Wagner @ 2003-02-01  6:35 UTC (permalink / raw)
  To: caml-list

I'm looking for a way, in pure ocaml without having to bail out to C, to
tell if a float is negative or not.

Just using x < 0.0 won't work, as I need to be able to tell the difference
between -0.0 and +0.0. This is for ocaml versions of the C copysign and
signbit functions. Any suggestions?

-- 
Shawn Wagner
shawnw@speakeasy.org
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Finding the sign of a float
  2003-02-01  6:35 [Caml-list] Finding the sign of a float Shawn Wagner
@ 2003-02-01  9:05 ` Chris Hecker
  2003-02-01 12:56 ` John Carr
  1 sibling, 0 replies; 5+ messages in thread
From: Chris Hecker @ 2003-02-01  9:05 UTC (permalink / raw)
  To: Shawn Wagner, caml-list


>I'm looking for a way, in pure ocaml without having to bail out to C, to
>tell if a float is negative or not.
>Just using x < 0.0 won't work, as I need to be able to tell the difference
>between -0.0 and +0.0. This is for ocaml versions of the C copysign and
>signbit functions. Any suggestions?

let is_neg v =
(Int64.shift_right_logical (Int64.bits_of_float v) 63) = Int64.one

Chris

PS.  My kingdom for overloaded arithmetic operators and Int32/64 constants.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Finding the sign of a float
  2003-02-01  6:35 [Caml-list] Finding the sign of a float Shawn Wagner
  2003-02-01  9:05 ` Chris Hecker
@ 2003-02-01 12:56 ` John Carr
  2003-02-01 14:11   ` malc
  1 sibling, 1 reply; 5+ messages in thread
From: John Carr @ 2003-02-01 12:56 UTC (permalink / raw)
  To: Shawn Wagner; +Cc: caml-list


> I'm looking for a way, in pure ocaml without having to bail out to C, to
> tell if a float is negative or not.
> 
> Just using x < 0.0 won't work, as I need to be able to tell the difference
> between -0.0 and +0.0. This is for ocaml versions of the C copysign and
> signbit functions. Any suggestions?

x < 0.0 || (x = 0.0 && 1.0/.x < 0.0)

may be faster than converting the bits to integer, or slower,
depending on platform and context, and how often you encounter -0.

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Finding the sign of a float
  2003-02-01 12:56 ` John Carr
@ 2003-02-01 14:11   ` malc
  2003-02-01 18:05     ` Shawn Wagner
  0 siblings, 1 reply; 5+ messages in thread
From: malc @ 2003-02-01 14:11 UTC (permalink / raw)
  To: John Carr; +Cc: Shawn Wagner, caml-list

On Sat, 1 Feb 2003, John Carr wrote:

>
> > I'm looking for a way, in pure ocaml without having to bail out to C, to
> > tell if a float is negative or not.
> >
> > Just using x < 0.0 won't work, as I need to be able to tell the difference
> > between -0.0 and +0.0. This is for ocaml versions of the C copysign and
> > signbit functions. Any suggestions?
>
> x < 0.0 || (x = 0.0 && 1.0/.x < 0.0)
>
> may be faster than converting the bits to integer, or slower,
> depending on platform and context, and how often you encounter -0.

1.0 /. x = neg_infinity less typing

-- 
mailto:malc@pulsesoft.com
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

* Re: [Caml-list] Finding the sign of a float
  2003-02-01 14:11   ` malc
@ 2003-02-01 18:05     ` Shawn Wagner
  0 siblings, 0 replies; 5+ messages in thread
From: Shawn Wagner @ 2003-02-01 18:05 UTC (permalink / raw)
  To: caml-list

On Sat, Feb 01, 2003 at 05:11:10PM +0300, malc wrote:
> On Sat, 1 Feb 2003, John Carr wrote:
> 
> >
> > > I'm looking for a way, in pure ocaml without having to bail out to C, to
> > > tell if a float is negative or not.
> > >
> > > Just using x < 0.0 won't work, as I need to be able to tell the difference
> > > between -0.0 and +0.0. This is for ocaml versions of the C copysign and
> > > signbit functions. Any suggestions?
> >
> > x < 0.0 || (x = 0.0 && 1.0/.x < 0.0)
> >
> > may be faster than converting the bits to integer, or slower,
> > depending on platform and context, and how often you encounter -0.
> 
> 1.0 /. x = neg_infinity less typing
> 

That one doesn't work for all negative numbers, though. I think I'll go
with John's, though Chris's suggestion of converting to an int64 is nifty. I
wasn't aware of bits_to_float before.

PS: I agree with Chris's sentiments about overloaded arithmetic operators
(It has to be possible; < > and = do it...) and largeint constants. That
last one is particularily annoying, as I never could figure out how to make
my patch to add them work with camlp4.

-- 
Shawn Wagner
shawnw@speakeasy.org
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


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

end of thread, other threads:[~2003-02-01 17:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-01  6:35 [Caml-list] Finding the sign of a float Shawn Wagner
2003-02-01  9:05 ` Chris Hecker
2003-02-01 12:56 ` John Carr
2003-02-01 14:11   ` malc
2003-02-01 18:05     ` Shawn Wagner

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 NNTP newsgroup(s).