T
tron.thomas
This C program:
#include <stdio.h>
#include <math.h>
int main()
{
float value;
for(value = -1.0f; 1.1f > value; value += 0.1f){
printf("%.1f\n", value);
}
return 0;
}
Produces this output:
-1.0
-0.9
-0.8
-0.7
-0.6
-0.5
-0.4
-0.3
-0.2
-0.1
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
Trying to write an equivalent C++ program using ostream has proven
tricky. Here is a solution I came up with:
#include <iostream>
#include <cmath>
int main()
{
static const float TOLERANCE = 0.0001f;
using std::cout;
cout << std::showpoint;
for(float value = -1.0f; 1.1f > value; value += 0.1f){
cout.precision(1);
float integral = ::floor(value);
if(TOLERANCE > ::fabsf(value - integral)){
value = integral;
cout.precision(2);
}
cout << value << '\n';
}
return 0;
}
It seems like a lot more work to accomplish the same thing. I wonder
if there is a better approach
What is the best way to produce this kind of output using an ostream
object?
#include <stdio.h>
#include <math.h>
int main()
{
float value;
for(value = -1.0f; 1.1f > value; value += 0.1f){
printf("%.1f\n", value);
}
return 0;
}
Produces this output:
-1.0
-0.9
-0.8
-0.7
-0.6
-0.5
-0.4
-0.3
-0.2
-0.1
0.0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1.0
Trying to write an equivalent C++ program using ostream has proven
tricky. Here is a solution I came up with:
#include <iostream>
#include <cmath>
int main()
{
static const float TOLERANCE = 0.0001f;
using std::cout;
cout << std::showpoint;
for(float value = -1.0f; 1.1f > value; value += 0.1f){
cout.precision(1);
float integral = ::floor(value);
if(TOLERANCE > ::fabsf(value - integral)){
value = integral;
cout.precision(2);
}
cout << value << '\n';
}
return 0;
}
It seems like a lot more work to accomplish the same thing. I wonder
if there is a better approach
What is the best way to produce this kind of output using an ostream
object?