Skip to content

Instantly share code, notes, and snippets.

@run4flat
Last active December 31, 2015 13:18
Show Gist options
  • Save run4flat/7991591 to your computer and use it in GitHub Desktop.
Save run4flat/7991591 to your computer and use it in GitHub Desktop.
Weird XSness fails to fail
package ToTest;
use 5.008_008;
use strict;
use warnings;
our $VERSION = '0.01';
require XSLoader;
XSLoader::load('ToTest', $VERSION);
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
MODULE = ToTest PACKAGE = ToTest
void
check_float_stuff(SV * constant_SV)
PROTOTYPE: $
PREINIT:
float real_const, constant;
double constant_d;
CODE:
real_const = atof(SvPV_nolen(constant_SV));
printf("The string interpretation of constant_SV is %s; the floating point is %f\n",
SvPV_nolen(constant_SV), SvNV_nomg(constant_SV));
constant = (float) SvNV_nomg(constant_SV);
constant_d = SvNV_nomg(constant_SV);
printf(" -> when cast to a float, we have %f; to a double, %f\n", constant, constant_d);
printf(" -> when directly printed as %%f: %f\n", SvNV(constant_SV));
printf(" -> real_constant is %f\n", real_const);
sv_dump(constant_SV);
use strict;
use warnings;
use ToTest;
ToTest::check_float_stuff(5);
ToTest::check_float_stuff(0.25);
@bulk88
Copy link

bulk88 commented Dec 16, 2013

On Win32 Visual C Perl 5.19.7/blead

C:\Documents and Settings\Owner\Desktop\cpan libs\tkt>perl float.pl
The string interpretation of constant_SV is 5; the floating point is 5.000000
 -> when cast to a float, we have 5.000000; to a double, 5.000000
 -> when directly printed as %f: 5.000000
 -> real_constant is 5.000000
SV = PVNV(0x9e995c) at 0x8fcaa4
  REFCNT = 1
  FLAGS = (IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
  IV = 5
  NV = 5
  PV = 0x8f4e44 "5"\0
  CUR = 1
  LEN = 12
The string interpretation of constant_SV is 0.25; the floating point is 0.250000

 -> when cast to a float, we have 0.250000; to a double, 0.250000
 -> when directly printed as %f: 0.250000
 -> real_constant is 0.250000
SV = PVNV(0x9e9974) at 0x8fca44
  REFCNT = 1
  FLAGS = (NOK,READONLY,pNOK)
  IV = 0
  NV = 0.25
  PV = 0x90c604 "0.25"\0
  CUR = 4
  LEN = 36

C:\Documents and Settings\Owner\Desktop\cpan libs\tkt>

@bulk88
Copy link

bulk88 commented Dec 16, 2013

C:\Documents and Settings\Owner\Desktop\cpan libs\tkt>perl float.pl
The string interpretation of constant_SV is 5; the floating point is 5.000000
 -> when cast to a float, we have 5.000000; to a double, 5.000000
 -> when directly printed as %f: 5.000000
 -> real_constant is 5.000000
SV = PVNV(0x19ab16c) at 0x182c01c
  REFCNT = 1
  FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
  IV = 5
  NV = 5
  PV = 0x185c3d4 "5"\0
  CUR = 1
  LEN = 4
The string interpretation of constant_SV is 0.25; the floating point is 0.250000

 -> when cast to a float, we have 0.250000; to a double, 0.250000
 -> when directly printed as %f: 0.250000
 -> real_constant is 0.250000
SV = PVNV(0x19ab1cc) at 0x182c05c
  REFCNT = 1
  FLAGS = (PADTMP,NOK,POK,READONLY,pNOK,pPOK)
  IV = 0
  NV = 0.25
  PV = 0x19b608c "0.25"\0
  CUR = 4
  LEN = 36

C:\Documents and Settings\Owner\Desktop\cpan libs\tkt>perl -v

This is perl, v5.10.0 built for MSWin32-x86-multi-thread
(with 5 registered patches, see perl -V for more detail)

Copyright 1987-2007, Larry Wall

Binary build 1003 [285500] provided by ActiveState http://www.ActiveState.com
Built May 13 2008 16:52:49

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.


C:\Documents and Settings\Owner\Desktop\cpan libs\tkt>

@run4flat
Copy link
Author

Compiled with normal C compiler (Perl v 5.18.1), Debian

The string interpretation of constant_SV is 5; the floating point is 5.000000
  -> when cast to a float, we have 5.000000; to a double, 5.000000
  -> when directly printed as %f: 5.000000
  -> real_constant is 5.000000
SV = PVNV(0x9834fd0) at 0x97b6310
  REFCNT = 1
  FLAGS = (IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
  IV = 5
  NV = 5
  PV = 0x97b0f38 "5"\0
  CUR = 1
  LEN = 12
The string interpretation of constant_SV is 0.25; the floating point is 0.250000
  -> when cast to a float, we have 0.250000; to a double, 0.250000
  -> when directly printed as %f: 0.250000
  -> real_constant is 0.250000
SV = PVNV(0x9834fe4) at 0x97b62a0
  REFCNT = 1
  FLAGS = (NOK,POK,READONLY,pNOK,pPOK)
  IV = 0
  NV = 0.25
  PV = 0x97c09b0 "0.25"\0
  CUR = 4
  LEN = 36

@run4flat
Copy link
Author

Perl v5.18.1, g++, Debian:

perl -Mblib test-xs.pl 
The string interpretation of constant_SV is 5; the floating point is 5.000000
  -> when cast to a float, we have 5.000000; to a double, 5.000000
  -> when directly printed as %f: 5.000000
  -> real_constant is 5.000000
SV = PVNV(0x9ed6fd0) at 0x9e58310
  REFCNT = 1
  FLAGS = (IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
  IV = 5
  NV = 5
  PV = 0x9e52f38 "5"\0
  CUR = 1
  LEN = 12
The string interpretation of constant_SV is 0.25; the floating point is 0.250000
  -> when cast to a float, we have 0.250000; to a double, 0.250000
  -> when directly printed as %f: 0.250000
  -> real_constant is 0.250000
SV = PVNV(0x9ed6fe4) at 0x9e582a0
  REFCNT = 1
  FLAGS = (NOK,POK,READONLY,pNOK,pPOK)
  IV = 0
  NV = 0.25
  PV = 0x9eef248 "0.25"\0
  CUR = 4
  LEN = 36

@run4flat
Copy link
Author

Perl v5.18.1, ExtUtils::nvcc as the compiler, Debian:

perl -Mblib test-xs.pl 
The string interpretation of constant_SV is 5; the floating point is 5.000000
  -> when cast to a float, we have 0.000000; to a double, 0.000000
  -> when directly printed as %f: 0.000000
  -> real_constant is 5.000000
SV = PVNV(0x876bfc0) at 0x86ed308
  REFCNT = 1
  FLAGS = (IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
  IV = 5
  NV = 5
  PV = 0x86e7f30 "5"\0
  CUR = 1
  LEN = 12
The string interpretation of constant_SV is 0.25; the floating point is 0.000000
  -> when cast to a float, we have 0.000000; to a double, 0.000000
  -> when directly printed as %f: 0.000000
  -> real_constant is 0.250000
SV = PVNV(0x876bfd4) at 0x86ed298
  REFCNT = 1
  FLAGS = (NOK,POK,READONLY,pNOK,pPOK)
  IV = 0
  NV = 0.25
  PV = 0x8784238 "0.25"\0
  CUR = 4
  LEN = 36

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment