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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:57:49+00:00 2026-06-03T10:57:49+00:00

I was writing a program to calculate negative powers of 2. I used the

  • 0

I was writing a program to calculate negative powers of 2.
I used the following two code snippets:

cout.precision(3);
cout << scientific << pow(2.0, p) << endl;

AND

ans = pow(2.0, p);
printf("%.3e\n", ans);

For p = -8271, the cout gives the right answer (1.517e-2490) but I get a widely different answer for the printf (6.929e-310). Why does this discrepancy occur?

I use Codeblocks on Ubuntu.

  • 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-03T10:57:57+00:00Added an answer on June 3, 2026 at 10:57 am

    I bet that’s because ans is a long double, but you didn’t tell printf to expect a long double. The format code you want is %.3Le assuming that’s the case.

    The g++ compiler even has a warning to detect format/parameter mismatches (I think it comes with -Wall) but I always prefer iostreams because they’re type safe like this.

    All this is of course assuming that p is also long double, causing the compiler to pick the long double version of pow.

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

Sidebar

Related Questions

I am writing a program to calculate the shortest path between two nodes. In
I'm writing a program (in C) in which I try to calculate powers of
So I was writing this simple program to calculate the day of any date
I'm writing a python program. The program calculates Latin Squares using two numbers the
I was writing a program to concatenate two arrays in C. I am allocating
I am writing a program in C++ where I have to calculate the value
I am writing a program to calculate payrolls for employees. What I need to
I am writing a simple program to calculate the average of a set of
I'm writing a small program to calculate a physics problem but I'm having problems
I'm writing a basic chess program to calculate how many sets you can make

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.