Archived

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

Ryan Hutchings

n00b question: truncating decimal integers, why??

Recommended Posts

Ryan Hutchings    122
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
Agape    122
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
bastardos    100

''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
Ryan Hutchings    122
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
Nervo    344
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