[#4766] Wiki — "Glen Stampoultzis" <trinexus@...>

21 messages 2000/09/04
[#4768] RE: Wiki — "NAKAMURA, Hiroshi" <nahi@...> 2000/09/04

Hi, Glen,

[#4783] Re: Wiki — Masatoshi SEKI <m_seki@...> 2000/09/04

[#4785] Re: Wiki — "NAKAMURA, Hiroshi" <nakahiro@...> 2000/09/05

Howdy,

[#4883] Re-binding a block — Dave Thomas <Dave@...>

16 messages 2000/09/12

[#4930] Perl 6 rumblings -- RFC 225 (v1) Data: Superpositions — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/09/15

[#4936] Ruby Book Eng. translation editor's questions — Jon Babcock <jon@...>

20 messages 2000/09/16

[#5045] Proposal: Add constants to Math — Robert Feldt <feldt@...>

15 messages 2000/09/21

[#5077] Crazy idea? infix method calls — hal9000@...

This is a generalization of the "in" operator idea which I

17 messages 2000/09/22

[#5157] Compile Problem with 1.6.1 — Scott Billings <aerogems@...>

When I try to compile Ruby 1.6.1, I get the following error:

15 messages 2000/09/27

[ruby-talk:5156] Re: Proposal: Add constants to Math

From: Robert Feldt <feldt@...>
Date: 2000-09-27 14:33:57 UTC
List: ruby-talk #5156
Hi,

Here's some really ugly/brutal code that seems to do the trick (with 
NaN, Inf and -Inf). I've successfully tested on both a big and a small
endian machine. Would be great if you could test it on your
machine/setup. Extract 3 files below or get tiny tarball at:

www.ce.chalmers.se/~feldt/ruby/extensions/floatconsts/floatconsts.tar.gz

Matz: Feel free to rip the code and insert constants into Float (or where
you think they fit)... ;-)

Regards,

Robert

Ps. Please read header comment in floatconsts.c in the tarball if you need
more info...

------------------------floatconsts.c
#include <ruby.h>
#include <math.h>

void Init_floatconsts()
{
  static unsigned char nan_small_endian_bytes[8] = 
    {0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  static unsigned char inf_small_endian_bytes[8] = 
    {0x7f, 0xf0, 0, 0, 0, 0, 0, 0};
  static unsigned char neginf_small_endian_bytes[8] = 
    {0xff, 0xf0, 0, 0, 0, 0, 0, 0};
  static unsigned char nan_big_endian_bytes[8] = 
    {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f};
  static unsigned char inf_big_endian_bytes[8] = 
    {0, 0, 0, 0, 0, 0, 0xf0, 0x7f};
  static unsigned char neginf_big_endian_bytes[8] = 
    {0, 0, 0, 0, 0, 0, 0xf0, 0xff};
  double nan, inf, neginf;
  static int i = 1;

  // Try the IEEE 754 floating point standard ways
  nan = 0.0/0.0;
  inf = 1.0/0.0;
  neginf = -1.0/0.0;

  // If at least one didn't work we try the brutal way!
  if( !isnan(nan) || !isinf(inf) || !isinf(neginf) ) {
    // Test endianness
    if( 1 == *((char*)&i) ) {
      // Big endian
      nan = *((double*)nan_big_endian_bytes);
      inf = *((double*)inf_big_endian_bytes);
      neginf = *((double*)neginf_big_endian_bytes);
    } else {
      // Small endian
      nan = *((double*)nan_small_endian_bytes);
      inf = *((double*)inf_small_endian_bytes);
      neginf = *((double*)neginf_small_endian_bytes);
    }
  }

  // If even this failed we return without setting them up
  if( !isnan(nan) || !isinf(inf) || !isinf(neginf) ) {
    printf("Error: Could not add constants\n");
    return;
  }

  // Set up constants
  rb_define_const(rb_cFloat, "NAN", rb_float_new(nan));
  rb_define_const(rb_cFloat, "INF", rb_float_new(inf));
  rb_define_const(rb_cFloat, "NEGINF", rb_float_new(neginf));
}

-----------------extconf.rb
require "mkmf"
$objs = ["floatconsts.o"]
create_makefile("floatconsts")

-----------------test_floatconsts.rb
require 'floatconsts'
if (Float::NAN != nil) and (Float::NAN.nan?) and
   (Float::INF != nil) and (Float::INF.infinite? == 1) and
   (Float::NEGINF != nil) and (Float::NEGINF.infinite? == -1)
  puts "Test successfull!"
else
  puts "ERROR: Test failed!"
end



In This Thread