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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:53:31+00:00 2026-06-11T21:53:31+00:00

I want to control the precision for a double during a comparison, and then

  • 0

I want to control the precision for a double during a comparison, and then come back to default precision, with C++.

I intend to use setPrecision() to set precision. What is then syntax, if any, to set precision back to default?

I am doing something like this

std::setPrecision(math.log10(m_FTOL));

I do some stuff, and I would like to come back to default double comparison right afterwards.

I modified like this, and I still have some errors

std::streamsize prec = std::ios_base::precision();
std::setprecision(cmath::log10(m_FTOL));

with cmath false at compilation, and std::ios_base also false at compilation. Could you help?

  • 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-11T21:53:32+00:00Added an answer on June 11, 2026 at 9:53 pm

    You can get the precision before you change it, with std::ios_base::precision and then use that to change it back later.

    You can see this in action with:

    #include <ios>
    #include <iostream>
    #include <iomanip>
    
    int main (void) {
        double pi = 3.141592653590;
    
        std::streamsize ss = std::cout.precision();
        std::cout << "Initial precision = " << ss << '\n';
    
        std::cout << "Value = " << pi << '\n';
    
        std::cout.precision (10);
        std::cout << "Longer value = " << pi << '\n';
    
        std::cout.precision (ss);
        std::cout << "Original value = " << pi << '\n';
    
        std::cout << "Longer and original value = "
            << std::setprecision(10) << pi << ' '
            << std::setprecision(ss) << pi << '\n';
    
        std::cout << "Original value = " << pi << '\n';
    
        return 0;
    }
    

    which outputs:

    Initial precision = 6
    Value = 3.14159
    Longer value = 3.141592654
    Original value = 3.14159
    Longer and original value = 3.141592654 3.14159
    Original value = 3.14159
    

    The code above shows two ways of setting the precision, first by calling std::cout.precision (N) and second by using a stream manipulator std::setprecision(N).


    But you need to keep in mind that the precision is for outputting values via streams, it does not directly affect comparisons of the values themselves with code like:

    if (val1 == val2) ...
    

    In other words, even though the output may be 3.14159, the value itself is still the full 3.141592653590 (subject to normal floating point limitations, of course).

    If you want to do that, you’ll need to check if it’s close enough rather than equal, with code such as:

    if ((fabs (val1 - val2) < 0.0001) ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I use variables to set my timer? I want to control delay
I'm doing a program to set the precision control of the FPU to 24
I want to control internet connection programmatically, (I use Visual Studio .net (C#)). I
I want to control the mouse pointer with my application and be able to
I want to control does an element exist in document with its ID, when
i want to control my tab pages with custom buttons...now i want to hide
I want to control domainUpDown as spinner to control value from 0 to 800
I want to control the x and y axis of a multi series line
I'm currently working on a project where I want to control a microprocessor (Arduino)
I am building a custom web panel control for specific purpose.I want the control

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.