/*
Copyright (C) 2001-2006, The Perl Foundation.
$Id: perlnum.pmc 16836 2007-01-30 04:30:52Z coke $

=head1 NAME

src/dynpmc/perlnum.pmc - Perl Floating-Point Number

=head1 DESCRIPTION

These are the vtable functions for the PerlNum base class

=head2 Methods

=over 4

=cut

*/

#include "parrot/parrot.h"

void Parrot_perlscalar_morph(Interp* , PMC* pmc, INTVAL type);

pmclass PerlNum
    extends Float
    does float
    does scalar
    dynpmc
    group perl_group
    hll Perl
    maps Float {

/*

=item C<STRING *get_string()>

Returns the number as a Parrot string.

=cut

*/

    STRING* get_string() {
#if 0
        double d = (double) PMC_num_val(SELF);
        const char *sign = "-";
        if (!signbit(PMC_num_val(SELF)))
            sign = "";
        d = fabs(d);
        return Parrot_sprintf_c(INTERP, "%s" FLOATVAL_FMT, sign, d);
#else
        /* XXX signbit isn't portable and as we are calling Parrot_sprintf_c
        * anyway, we can use the builtin number formatting too
        * this might still be a problem with -0.0
        */
        return Parrot_sprintf_c(INTERP, "%Pf", SELF);
#endif
    }


/*

=item C<void set_integer_native(INTVAL value)>

=cut

*/

    void set_integer_native(INTVAL value) {
        INTVAL dynpmc_PerlInt;

        dynpmc_PerlInt = pmc_type(interp,
            string_from_const_cstring(interp, "PerlInt", 0));
        DYNSELF.morph(dynpmc_PerlInt);
        DYNSELF.set_integer_native(value);
    }

/*

=item C<void set_number_native(FLOATVAL value)>

Sets the value of the number to C<value>.

Note that if C<value> is an integer the number morphs to a C<PerlInt>.

=cut

*/

    void set_number_native(FLOATVAL value) {
        INTVAL vali = (INTVAL) value;

        PMC_num_val(SELF) = value;
        /* don't mess around with - 0 */
        if (value == vali && (vali || !Parrot_signbit(value)))
            DYNSELF.set_integer_native(vali);
    }

/*

=item C<void set_string_native(STRING *value)>

Sets the value of the number to the value of C<*value>.

Note that this method morphs the number into a C<PerlString>.

=cut

*/

    void set_string_native(STRING * value) {
        INTVAL dynpmc_PerlString;

        dynpmc_PerlString = pmc_type(interp,
            string_from_const_cstring(interp, "PerlString", 0));
        DYNSELF.morph(dynpmc_PerlString);
        DYNSELF.set_string_native(value);
    }

/*

=item C<void morph(INTVAL type)>

Morphs the scalar to the specified type.

=cut

*/

    void morph(INTVAL type) {
        perlscalar.SELF.morph(type);
    }
}

/*

=back

=cut

*/


/*
 * Local variables:
 *   c-file-style: "parrot"
 * End:
 * vim: expandtab shiftwidth=4:
 */
