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

  • Home
  • SEARCH
  • 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 530985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:15:14+00:00 2026-05-13T09:15:14+00:00

First my context is that of a compiler writer who needs to convert floating

  • 0

First my context is that of a compiler writer who needs to convert floating point literals (strings) into float/double values. I haven’t done any floating point programming the last 15 years so i’m pretty sure this is a total stupid newbie question.

double res; 
errno=0; 
*res = strtod((const char*)literal,NULL); 
if (errno==ERANGE) throw_a_literal_out_of_range_exception();
return res;

but there is no “strtof” function in the c runtime library?

EDIT: To clarify. I already know that the string ‘literal’ is a valid pattern for a C floating point. It already passed a regular expression test. I just want to check if there is a range/precision problem.

The reason is in Eiffel source code a user can write

a := {REAL_32} 3.1415
b := {REAL_32} 3.0E200

To cast a written floating point number explit to a 32bit floating point. I think in the second case the compiler should detect that the value is out of range and raise an error or at least a warning.

  • 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-13T09:15:14+00:00Added an answer on May 13, 2026 at 9:15 am

    Since you have your value in a double, you can just check if it’s outside of the range of float:

    #include <stdlib.h>
    #include <stdio.h>
    #include <float.h>
    
    int main(int argc, char *argv[])
    {
        double d = FLT_MAX;
        if (argc > 1) {
            d = strtod(argv[1], NULL);
        }
        if ((d > 0 && (d > FLT_MAX || d < FLT_MIN))
                    || (d < 0 && (d < -FLT_MAX || d > -FLT_MIN)))
            printf("Invalid float: %g\n", d);
        else
            printf("Valid: %g\n", d);
    
        return EXIT_SUCCESS;
    }
    

    Running the program:

    $ ./a.out
    Valid: 3.40282e+38
    $ ./a.out 3.5e38
    Invalid float: 3.5e+38
    $ ./a.out -1e40
    Invalid float: -1e+40
    

    You may or may not need to add a test for correct strtod() return, depending upon whether there’s a possibility of an overflow in double type as well.

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

Sidebar

Related Questions

First, some context: I'm a Python developer who has written a medium-sized application using
first of all, I hope that I ask the question in the right context
First of all, I do understand, that MVC 3 default template's Content folder is
I want to replace the first context of web/style/clients.html with the java String.replaceFirst method
first i explain the context then the problem. I am using jstree to transform
First of all, the context of this question is: Cannot install psycopg2 on OSX
First, a bit of context: web.xml: I match all /app/* requests to my dispatcher:
A bit of context is probably necessary first, Basically I've been using the VS2008
I have this var manager = context.ManagerInfoes.Select(m => m.Guid == managerGuid).First(); how can i
I'm working on a method that needs to repeat a small operation at different

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.