Float types

Hello!

Shouldn’t:

if (TYPE(arg1) == T_FLOAT)

be true when someone passes ‘42’ (for example) and not ‘42.0’.

I understand TYPE(arg1) != T_FIXNUM when someone passes 42.0,
but I expect “42” to be a valid float argument.

I have foo(double i); and I check the passed arguments. I expect the
right ruby implementation to accept a foo(42) call.

Regards,
Elias

if (TYPE(arg1) == T_FLOAT)

Write it

    arg1 = rb_Float(arg1);

Guy Decoux

Hi,

Shouldn’t:

if (TYPE(arg1) == T_FLOAT)

be true when someone passes ‘42’ (for example) and not ‘42.0’.

No, Ruby C API do not processing mathematics. It handles structure of
data. It doesn’t care whether integer 42 can be seen as float in
math.

I have foo(double i); and I check the passed arguments. I expect the
right ruby implementation to accept a foo(42) call.

As Guy stated, call rb_Float() before invoking foo(double);

						matz.
···

In message “float types” on 04/01/28, elathan@phys.uoa.gr elathan@phys.uoa.gr writes:

Quoting ts decoux@moulon.inra.fr:

if (TYPE(arg1) == T_FLOAT)

Write it

arg1 = rb_Float(arg1); 

Actually I want to check the arguments’ types before calling the
appopriate (overloaded) C++ function. So, I want a check that
‘42’ and ‘42.0’ passes and a check that ‘42’ only passes. So, if
I have:

foo (char *, double, double);
foo (double, int, int);

I can then call in ruby:

foo 42, 20, 20
foo 42.0, 20, 20
foo “bar”, 10, 1.2

If this isn’t supported by the Ruby API I will write my own C macro that
will return true if TYPE == T_FLOAT || TYPE == T_FIXNUM.

Regards,
Elias

elathan@phys.uoa.gr wrote:

Actually I want to check the arguments’ types before calling the
appopriate (overloaded) C++ function. So, I want a check that
‘42’ and ‘42.0’ passes and a check that ‘42’ only passes.

For the more general case (where both 42 and 42.0 pass), perhaps you
could check to see if the argument is a Numeric, i.e.

 if (Qtrue == rb_obj_is_kind_of(arg1, rb_cNumeric)) {
     /* could be an Integer, Float, or other numeric type */
 } else if (TYPE(arg1) == T_FIXNUM) {
     /* it's definitely a Fixnum) */
 }

If you need to specifically check for (Fixnum || Float) and no other
Numeric types are supported then yes, you may need to roll a little
custom macro for that case.

Hope this helps,

Lyle

Hi,

If this isn’t supported by the Ruby API I will write my own C macro that
will return true if TYPE == T_FLOAT || TYPE == T_FIXNUM.

One thought:

if (!NIL_P(val = rb_check_convert_type(arg1, T_STRING,
				   "String", "to_str"))) {
foo(RSTRING(val)->ptr, RFLOAT(rb_Float(arg2))->value,
    RFLOAT(rb_Float(arg3))->value);
}
else if (!NIL_P(val = rb_check_convert_type(arg1, T_FLOAT,
					"Float", "to_float"))) {
foo(RFLOAT(val)->value, NUM2INT(arg2), NUM2INT(arg3));
}
else {
rb_raise(rb_eArgError, "unsupported argument %s",
	 rb_obj_classname(arg1));
}
···

At Wed, 28 Jan 2004 19:00:09 +0900, elathan@phys.uoa.gr wrote:


Nobu Nakada