Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Ryan Hutchings

n00b question: truncating decimal integers, why??

This topic is 5323 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i can''t really figure out why this is happening and hoped someone could help.. writing in C++, i''m trying to declare a constant for a decimal integer: const float pi = 3.14159; but everytime i run the program, it truncates my answer, rounding 3.14159 up to 3 prior to completing the equation. Obviously i wish to make the pi constant equal not 3, but 3.14159. why is this occuring?

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Ryan Hutchings
i can''t really figure out why this is happening and hoped someone could help..

writing in C++, i''m trying to declare a constant for a decimal integer:

const float pi = 3.14159;

but everytime i run the program, it truncates my answer, rounding 3.14159 up to 3 prior to completing the equation. Obviously i wish to make the pi constant equal not 3, but 3.14159. why is this occuring?


When you use pi, are you using it with integers? Like 5 * pi? I think that truncates the answer, unless you do 5.0 * pi.

Share this post


Link to post
Share on other sites

''tis true. When multiplying ''pi'' with an integer immediate, do:

float value = pi * 2.0;

you can also do:

float value = pi * (float)2;

You need to use the latter form when multiplying pi by an integer variable.

.bas

[sPiKie] mmorpg isnt hard in vb

Share this post


Link to post
Share on other sites
i''m using it like so:

#include <iostream.h>
#include <stdlib.h>

int main()
{
//need to fix float, etc.
const float pi = 3.14159;
cout << "Circumfrence of a circle...\n";
cout << "\n";
cout << "Please enter the diameter of the circle: \n";
int a, c;
cout << "\?";
cin >> a;
cout << "\n";
c= pi * a;
cout << "The approximate circumfrence of the circle\nin question is:\t" << c << "\n";
cout << "\n";



system("pause");
return 0;
}



*bold are the problem areas... lol.

Share this post


Link to post
Share on other sites
quote:
Original post by Ryan Hutchings
i'm using it like so:

#include <iostream.h>
#include <stdlib.h>

int main()
{
//need to fix float, etc.
const float pi = 3.14159;
cout << "Circumfrence of a circle...\n";
cout << "\n";
cout << "Please enter the diameter of the circle: \n";
int a, c;
cout << "\?";
cin >> a;
cout << "\n";
c= pi * a;
cout << "The approximate circumfrence of the circle\nin question is:\t" << c << "\n";
cout << "\n";



system("pause");
return 0;
}



*bold are the problem areas... lol.


Um...

You have two problems...you are multiplying by an int and assigning to an int...change a and c to float a,c or double a,c

EDIT: Oh, and "circumfrence" is actually spelled circumference.

[edited by - nervo on January 24, 2004 4:03:55 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!