0

I am trying to create a sine, cosine, tangent, and cotangent table. I want to printf/cout an "INF" instead of huge complicated numbers or interesting symbols when I calculate the cotangent of 0. But it doesn't allow me to do this. I tried every way I can think of, but I couldn't do it.

Can you help me about that?

Code is below:

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <iostream>
#define PI 3.14159265
#include <math.h>
#include <string>
#include <sstream>
using namespace std;

int main()
{  
    setlocale(LC_ALL, "Turkish");

    string diziBaslik[1][5] = {{"AÇI","SİN","COS","TAN","COTAN"}};
    string diziBaslikCizgi[1][5] = {{"------","------","------","------","------"}};
    float dizi[10][5] = {
        {0 ,sin(0*PI/180) ,cos(0*PI/180) ,tan(0*PI/180) ,1/tan(0*PI/180) },
        {10,sin(10*PI/180),cos(10*PI/180),tan(10*PI/180),1/tan(10*PI/180)},
        {20,sin(20*PI/180),cos(20*PI/180),tan(20*PI/180),1/tan(20*PI/180)},
        {30,sin(30*PI/180),cos(30*PI/180),tan(30*PI/180),1/tan(30*PI/180)},
        {40,sin(40*PI/180),cos(40*PI/180),tan(40*PI/180),1/tan(40*PI/180)},
        {50,sin(50*PI/180),cos(50*PI/180),tan(50*PI/180),1/tan(50*PI/180)},
        {60,sin(60*PI/180),cos(60*PI/180),tan(60*PI/180),1/tan(60*PI/180)},
        {70,sin(70*PI/180),cos(70*PI/180),tan(70*PI/180),1/tan(70*PI/180)},
        {80,sin(80*PI/180),cos(80*PI/180),tan(80*PI/180),1/tan(80*PI/180)},
        {90,sin(90*PI/180),cos(90*PI/180),tan(90*PI/180),1/tan(90*PI/180)}
    };
    
    cout << diziBaslik[0][0] << "\t";
    
    for(int j=1;j<5;j++){
        cout << diziBaslik[0][j] << "\t\t";
    }
    
    cout<< endl;
    
    cout << diziBaslikCizgi[0][0] << "\t";
    
    for(int j=1;j<5;j++){
        cout << diziBaslikCizgi[0][j] << "\t\t";
    }
    
    cout << endl;
    for(int i=0;i<10;i++){
        for(int j=0;j<5;j++){
            if(j==0){
                cout << dizi[i][j] << "\xB0\t";
            }
            else{
                printf("%.6f\t", dizi[i][j]);
            }
        }
        cout<<endl;
    }
}
10
  • Both cout and printf show inf given float x = 1/tan(0*PI/180); cout << x; Commented Apr 5, 2021 at 19:43
  • Just replace 1/tan(0*PI/180) and tan(90*PI/180) with INFINITY. cout will print inf. Commented Apr 5, 2021 at 19:47
  • I think that your problem goes more with tan(90*PI/180) than with cotan(0) because your program already shows inf for cotan(0). The problem is with tan(90*PI/180)because you can't give enough precission to PI so that the tangent of that angle reaches infinity. I would go with the comment by rustyx, it's probably your best option. Commented Apr 5, 2021 at 19:52
  • Sidenote: You can do std::cout << std::setprecision(6) << std::fixed; if you want to use std::cout instead of std::printf("%.6f", ...). Commented Apr 5, 2021 at 20:15
  • @rustyx thanks for advice. I did it. Now it says "1,#INF00". Can I change it by "INFINITY" or any letter else? Commented Apr 5, 2021 at 20:19

1 Answer 1

2

There are 2 issues:

  1. Due to low PI precision, tan(90*PI/180) returns a large number and not infinity.
  2. Your system is printing infinity as 1,#INF00. I don't think there's a way to change that.

To work around both issues don't let large values go to printf and instead print your own string if the value is larger than some tolerance value:

        if (dizi[i][j] < 1e5) {
            printf("%.6f\t", dizi[i][j]);
        } else {
            printf("INFINITY\t");
        }

Also note that setlocale() won't affect cout because it's constructed before. For cout you need to add something like

  cout.imbue(std::locale("Turkish"));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.