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);
@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