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 6616657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:36:33+00:00 2026-05-25T20:36:33+00:00

I am working on converting parts of a C++ program to Python, but I

  • 0

I am working on converting parts of a C++ program to Python, but I have some trouble replacing the C function strtod.
The strings I’m working on consists of simple mathmatical-ish equations, such as “KM/1000.0”. The problem is that the both constants and numbers are mixed and I’m therefore unable to use float().

How can a Python function be written to simulate strtod which returns both the converted number and the position of the next character?

  • 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-05-25T20:36:34+00:00Added an answer on May 25, 2026 at 8:36 pm

    I’m not aware of any existing functions that would do that.

    However, it’s pretty easy to write one using regular expressions:

    import re
    
    # returns (float,endpos)
    def strtod(s, pos):
      m = re.match(r'[+-]?\d*[.]?\d*(?:[eE][+-]?\d+)?', s[pos:])
      if m.group(0) == '': raise ValueError('bad float: %s' % s[pos:])
      return float(m.group(0)), pos + m.end()
    
    print strtod('(a+2.0)/1e-1', 3)
    print strtod('(a+2.0)/1e-1', 8)
    

    A better overall approach might be to build a lexical scanner that would tokenize the expression first, and then work with a sequence of tokens rather than directly with the string (or indeed go the whole hog and build a yacc-style parser).

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

Sidebar

Related Questions

I'm working on converting a very simple java desktop application to run in java
I am working on converting the functionality of some JSP pages to GWT. I
I'm working on converting some NSURLConnection code over to AFNetworking and I'm seeing a
Working on converting an array to another array. In PHP, this is easy, but
I'm currently working on a project Euler problem (www.projecteuler.net) for fun but have hit
I am currently converting a very old, but working classic ASP site to ASP.Net.
I realise different solutions will have different variations of what Working Days means but
I'm working on converting a neural network simulator from Perl to C. I have
I am working on converting a Backbone application into an Ember application using Ember
I'm working on converting an old define()-based language/translation system to a more flexible one

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.