Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8229685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:52:51+00:00 2026-06-07T16:52:51+00:00

I’m learning SWIG, for using C in Python. I’ve written this function, but I

  • 0

I’m learning SWIG, for using C in Python. I’ve written this function, but I can’t understand, why the wrapped myfunc returns wrong float/double values:

mfuncs.c

#include <stdlib.h>

float myfunc(int n) {
    float result;
    result = 100 / n;
    return result;
}

mfuncs.i

%module mfuncs
%typemap(out) double, float "$result = PyFloat_FromDouble($1);"

extern float myfunc(int n);

Finally I get 1107558400.0 instead of 33.33333.

>>> import mfuncs
>>> mfuncs.myfunc(3)
1107558400.0
>>> 

Where is the mistake?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:52:53+00:00Added an answer on June 7, 2026 at 4:52 pm

    The SWIG typemap is unneeded – it’s provided by default, you only need to write typemaps for “esoteric” types and the default double/float ones provided are fine here.

    The real problem here is that you’re not compiling with warnings enabled or ignoring them! It’s really worth getting in the habit of compiling with “-Wall -Wextra” or whatever your compiler requires to enable maximum warnings and heeding them.

    Your SWIG interface only tells SWIG about the function myfunc but there’s nothing in that interface to make the declaration available to the compiler you use to compile the generated myfuncs_wrap.c. This means that when you come to compile the shared library you’re relying on an implicit declaration of myfunc. GCC on my machine with -Wall reports this:

    test_wrap.c:3139:3: warning: implicit declaration of function ‘myfunc’

    The implicit declaration assumes it returns int. That’s just the rule in C if there is no declaration, it’s as though you wrote:

    #include <stdlib.h>
    
    int myfunc(int n);
    
    int main() {
      printf("%d\n", myfunc(3));   
      return 0;
    }
    

    which is clearly wrong (undefined behaviour to be exact) given the definition of myfunc returns a float. Your implementation (legally) chooses to do the simplest thing for this undefined behaviour, which is roughly a bit-wise cast from int to float. (It could equally well do anything, even something different on every run – that’s the beauty of undefined behaviour).

    You can fix your SWIG interface by changing it to:

    %module mfuncs
    %{
    extern float myfunc(int n);
    %}
    
    extern float myfunc(int n);
    

    this works because the code between %{ and %} is directly passed to the generated wrapper, which makes the compiler aware of the real declaration of myfunc when building the wrapper.

    There’s a nicer solution in my view though: provide the declaration only once, in a header file and then your interface file becomes:

    %module mfuncs
    %{
    #include "myfunc.h"
    %}
    
    %include "myfunc.h"
    

    (and obviously #include "myfunc.h" in myfunc.c). In that way you only write the declaration once and the compiler will warn/error if there’s anything that’s not quite expected rather than just take a (usually wrong) best guess.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.