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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:03:00+00:00 2026-06-08T07:03:00+00:00

I am new to visual c++ and wrote the following c++ code. I was

  • 0

I am new to visual c++ and wrote the following c++ code. I was just doing some floating point multiplication nothing more :). But I have a problem.

#include "stdafx.h"
#define PI 3.14F
#define totalRound 10.00F

void MultiplyPIArrayStored()
{
    printf("\n\nAnalysis\n");

    float* defArr = new float[(int)totalRound];
    float inc=0.00F;
    for(float i=1.00F;i<=totalRound;i++)
    {
        defArr[(int)i]=i*PI;
        //printf("Calculation: #define => %f * %f = %f\n",i,PI,i*PI);
    }

    float lPI=3.14F;
    for(float i=1.00F;i<=totalRound;i++)
    {
        //printf("Calculation: local variable => %f * %f = %f\n",i,lPI,i*lPI);
        printf("#define =>%f; local variable=>%f\n",defArr[(int)i],i*lPI);
        if(defArr[(int)i]==i*lPI)
            inc++;
    }

    printf("\nequal rate %f percentage",(inc/totalRound)*100);
    printf("\ndifference rate %f percentage",((totalRound-inc)/totalRound)*100);
}

void MultiplyPI()
{
    printf("\n\nAnalysis\n\n");

    float lPI=3.14F;
    float inc=0.00F;

    for(float i=1.00F;i<=totalRound;i++)
    {
        printf("\nCalculation: #define => %f * %f = %f\n",i,PI,i*PI);
        printf("Calculation: local variable => %f * %f = %f\n",i,lPI,i*lPI);
        printf("#define => %f ; local variable => %f\n",i*PI,i*lPI);
        if(i*PI==i*lPI)
            inc++;
    }

    printf("\nEqual rate %f percentage",(inc/totalRound)*100);
    printf("\nDifference rate %f percentage",((totalRound-inc)/totalRound)*100);
}

int _tmain(int argc, _TCHAR* argv[])
{
    MultiplyPI();
    getchar();

    MultiplyPIArrayStored();
    getchar();

    return 0;
}

It is giving the following output.

Analysis


Calculation: #define => 1.000000 * 3.140000 = 3.140000
Calculation: local variable => 1.000000 * 3.140000 = 3.140000
#define => 3.140000 ; local variable => 3.140000

Calculation: #define => 2.000000 * 3.140000 = 6.280000
Calculation: local variable => 2.000000 * 3.140000 = 6.280000
#define => 6.280000 ; local variable => 6.280000

Calculation: #define => 3.000000 * 3.140000 = 9.420000
Calculation: local variable => 3.000000 * 3.140000 = 9.420000
#define => 9.420000 ; local variable => 9.420000

Calculation: #define => 4.000000 * 3.140000 = 12.560000
Calculation: local variable => 4.000000 * 3.140000 = 12.560000
#define => 12.560000 ; local variable => 12.560000

Calculation: #define => 5.000000 * 3.140000 = 15.700001
Calculation: local variable => 5.000000 * 3.140000 = 15.700001
#define => 15.700001 ; local variable => 15.700001

Calculation: #define => 6.000000 * 3.140000 = 18.840001
Calculation: local variable => 6.000000 * 3.140000 = 18.840001
#define => 18.840001 ; local variable => 18.840001

Calculation: #define => 7.000000 * 3.140000 = 21.980001
Calculation: local variable => 7.000000 * 3.140000 = 21.980001
#define => 21.980001 ; local variable => 21.980001

Calculation: #define => 8.000000 * 3.140000 = 25.120001
Calculation: local variable => 8.000000 * 3.140000 = 25.120001
#define => 25.120001 ; local variable => 25.120001

Calculation: #define => 9.000000 * 3.140000 = 28.260001
Calculation: local variable => 9.000000 * 3.140000 = 28.260001
#define => 28.260001 ; local variable => 28.260001

Calculation: #define => 10.000000 * 3.140000 = 31.400001
Calculation: local variable => 10.000000 * 3.140000 = 31.400001
#define => 31.400001 ; local variable => 31.400001

Equal rate 100.000000 percentage
Difference rate 0.000000 percentage


Analysis
#define =>3.140000; local variable=>3.140000
#define =>6.280000; local variable=>6.280000
#define =>9.420000; local variable=>9.420000
#define =>12.560000; local variable=>12.560000
#define =>15.700001; local variable=>15.700001
#define =>18.840000; local variable=>18.840001
#define =>21.980001; local variable=>21.980001
#define =>25.120001; local variable=>25.120001
#define =>28.260000; local variable=>28.260001
#define =>31.400002; local variable=>31.400001

equal rate 40.000000 percentage
difference rate 60.000000 percentage

Question: I am using a float array in the function’MultiplyPIArrayStored();‘. If you look at the output the value stored in array is changed (Please check the last part of the output.). Is there any problem in the array declaration? Why the array value is changing?

  • 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-08T07:03:01+00:00Added an answer on June 8, 2026 at 7:03 am

    The array is changing because that is the nature of floating point math. If you require exact values, don’t use floating point math. When floating point numbers are loaded and stored, they may be subject to precision extension or precision reduction. This can cause the ending digits to change.

    If you ask two people to write “1/3” as a decimal number, they may write different things. If you ask someone to write “1/3″ in six digits and double it and then ask someone else to write 2/3” in six digits, you may get “.333333” doubling to “.666666” but the person writing 2/3 might write “.6666667”. If you subtract 1/3 twice from 2/3. you may get .000001 left over. That’s the nature of approximate representations. Don’t use them if this is not the behavior you want/need.

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

Sidebar

Related Questions

I have a RichTextBox created programmatically with the following code: RichTextBox RT = new
I have the following code but I am getting an exception that a smtp
I am new to visual C++. I wrote my class, but it has error
I wrote a simple C code for displaying a matrix in visual studio but
I've just got the new Visual Studio 2010 Ultimate and Javascript Intellisense is not
I'm completely new to Visual Studio and I'm having some trouble getting a project
I'm new to Visual C# Studio (actually using the Express edition, but another developer
I'm new to visual studio, coming from Delphi. I have a directory tree full
So I wrote the following code in linux(Ubuntu) using the emacs text editor it
I have the following code: ofstream mOutFile.open(logPath, ios_base::app); string lBuilder; lBuilder.append(========================================================\n); lBuilder.append(Date: ); lBuilder.append(asctime(timeinfo));

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.