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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:10:24+00:00 2026-05-27T00:10:24+00:00

I need to parse floating-point literals in C code using OCaml. OCaml’s float type

  • 0

I need to parse floating-point literals in C code using OCaml.

OCaml’s float type is 64 bit. I have the string of the literal, its numeric value rounded to 64 bits and its kind (float, double or long double).

The problem are literals with a numeric value bigger than 64 bit:

  1. long double literals
  2. float literals with ‘f’-suffix for which double rounding errors would occur if they wouldn’t have the suffix.

OCaml’s arbitrary-precision module can parse rational numbers from strings like “123/123”, but not “123.123”, “123e123”, “0x1.23p-1” like they might appear in C.

Background: I do value analysis of C programs using CIL.

Double literals of any size and float literals with a numeric value that fits into 64 bit are always correctly represented. By rounding from double- to single-precision I can also reproduce double rounding errors.

  • 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-27T00:10:24+00:00Added an answer on May 27, 2026 at 12:10 am

    I wrote my answer in the form of a blog post

    To summarize some of the points here: you could interface strtold() and strtof() from OCaml. For the former, you would have to consider how you are going to store the result it produces, since there only is a point if long double is larger than double on your host architecture. There remains the problem that these functions are buggy in one of the most widely used C library. Very slightly buggy, but buggy for exactly the examples that are going to be of interest if you are doing this to study double rounding.

    Another way is to write your own function, starting from another post in the blog you refer to.

    Finally, the phrase “Even getting single-precision floats right requires me to parse literals with values bigger than 64 bit” that you use in the comments is still a strange way to put it. The intermediate format(s) in which you can parse the representation of a single-precision float before you round it to single-precision have to be lossless, otherwise there will be double rounding. Double rounding may be more or less difficult to exhibit depending on the precision of the lossy intermediate format, but using 80 bits or 128 bits binary floating-point formats is not going to remove the problem, just make it more subtle. In the simple algorithm that I recommend, the intermediate format is a fraction of two multiprecision integers.

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

Sidebar

Related Questions

gcc 4.6.1 c89 I have a string that I need parse. The string is
Need to parse a Class declaration line in Java using regular expression e.g. String
I need to parse a wstring to int, float or a string itself. I
I have a rather big number of source files that I need parse and
I need to parse a string like February 12, 1981 as a Date. I
I need to parse a bunch of incoming xml documents, they all have the
I need to parse out the string that has following structure x:{a,b,c,}, y:{d,e,f} etc.
I need parse a string inside a parenthesis, which looks like (A, B, C),
I need to parse C#, Ruby and Python source code to generate some reports.
I need to parse a string like func1(arg1, arg2); func2(arg3, arg4); . It's not

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.