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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:34:08+00:00 2026-06-17T17:34:08+00:00

I have the following problem : I write my code with the Qt IDE.

  • 0

I have the following problem : I write my code with the Qt IDE. I was informed that when people try to compile it with other IDE’s (like codeblocks, or visual studio) The output they get is different and that there are maufunctions. Any ideas what can be causing this ? I will give you an example:

This is Newton’s Method with a function who’s root is 2.83something. I get the same, correct calulations each time I run it in Qt. I get “nan” in code blocks and something irrelevant as well in visual studio. I don’t understand, do I have a mistake somewhere in my code ? What can be causing this ?

#include <iostream>
#include <cmath> // we need the abs() function for this program

using namespace std;

const double EPS = 1e-10; // the "small enough" constant. global variable, because it is good programming style

double newton_theorem(double x)
{
    double old_x = x; // asign the value of the previous iteration
    double f_x1 = old_x*old_x - 8; // create the top side of the f(x[n+1] equation
    double f_x2 = 2 * old_x; // create the bottom side
    double new_x = old_x -  f_x1 / f_x2; // calculate f(x[n+1])
    //cout << new_x << endl; // remove the // from this line to see the result after each iteration
    if(abs(old_x - new_x) < EPS) // if the difference between the last and this iteration is insignificant, return the value as a correct answer;
    {
        return new_x;
    }
    else // if it isn't run the same function (with recursion YAY) with the latest iteration as a starting X;
    {
        newton_theorem(new_x);
    }
}// newton_theorem

int main()
{
    cout << "This program will find the root of the function f(x) = x * x - 8" << endl;
    cout << "Please enter the value of X : ";
    double x;
    cin >> x;
    double root = newton_theorem(x);
    cout << "The approximate root of the function is: " << root << endl;

    return 0;
}//main
  • 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-17T17:34:10+00:00Added an answer on June 17, 2026 at 5:34 pm

    Yes, you run into undefined behavior:

    if(abs(old_x - new_x) < EPS) // if the difference between the last and this iteration is insignificant, return the value as a correct answer;
    {
        return new_x;
    }
    else // if it isn't run the same function (with recursion YAY) with the latest iteration as a starting X;
    {
        /*return*/ newton_theorem(new_x); // <<--- HERE!
    }
    

    Missing a return on the else branch.

    We could try to explain why Qt works (its compiler automatically puts the result of newton_theorem in the return registry, or shares registries, or whatever), but the fact of the matter is anything can happen. Some compilers might behave the same on subsequent runs, some might crash all the time.

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

Sidebar

Related Questions

When I try to write code like the following source code, Visual Studio doesn't
Hi i'm facing the following problem, i have to re-write an existant code to
Hi i have following Problem. I write a Mediawiki Extension where i need some
I have following problem, Code: String a=Yeahh, I have no a idea what's happening
hi i have write following code which prints elements in sorted order only one
I have the following code that is really bugging me, I'm thinking perhaps the
Ok hope that title wasn't misleading I have (among other columns) the following info
I have the following problem. I got a bat file that runs testcomplete test.
I have the following problem. I have the method that sends json via POST:
I have the following problem. I have a data set that has the beginning

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.