how to convert from double to float?

hi, i've created this code to get the sin(x) value, but it keeps crashing my program when i run it.

#include <iostream>
#include <cmath>
using namespace std;
double calc_sin(double x) {
return sin(x);
int main()
for (float x =0; x<2; x+=0.1) {
cout << x << "\t" << calc_sin(x) << endl; // call calc_sin()
return 0;

the bold part is where i'm having trouble.
What is the point of the calc_sin() function? All it does it call sin(). And what is the error when you program crashes? It looks okay to me. In any case, I think what you want is calc_sin(static_cast<double>(x)). This will force a conversion from float to double.

Also, I recommend you don't use for loops based on floats, since the impercision caused by the comparsion could cause the loop to run forever.
i was learning how to call the functions. i just changed all the doubles to float and it solved the problem. i will try your answer as well. thank you for the reply.
oh, the error when my program crashes was that it kept going at 0.909297
Topic archived. No new replies allowed.