Public Group

#### Archived

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

# n00b question: truncating decimal integers, why??

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

## 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 on other sites
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 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 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 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]

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 9
• 14
• 12
• 10
• ### Forum Statistics

• Total Topics
633273
• Total Posts
3011168
• ### Who's Online (See full list)

There are no registered users currently online

×