Chang
Chang

Reputation: 37

Borland C++, problems in getting decimals

I couldn't find a match to what I wanna ask. Why cant I display tan (39/180*3.142)? Instead it gives me 0.0000, I need the decimals.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    float h, a, d;
    char* number = new char[255];
    a = tan (39/180*3.142);
    sprintf(number, "%.6f", a);
    Height->Text = number;
}

Please advice. Thnx.

Upvotes: 1

Views: 1295

Answers (2)

Ólafur Waage
Ólafur Waage

Reputation: 70001

You are dividing two integers, they will give you zero if the result will be 0.something. So you need to cast one into a float.

(39 / (float)180 * 3.142);

Or use float numbers

(39.0f / 180.0f * 3.142);

Upvotes: 2

Yochai Timmer
Yochai Timmer

Reputation: 49261

try using float numbers, it might calculate 39/180 (integers) as 0:

a = tan (39f/180f *3.142);

Upvotes: 3

Related Questions