grinder said:
I have a program that fits a line to data points. The question is: can
'C' understand scientific notation (as below) , and if not what can i
do to make it work..
0.000 0.3231E+02 0.0000E+00 0.00 0.5642E+02
13.191 0.3421E+02 0.0000E+00 10.00 0.5910E+02
26.248 0.2842E+02 0.0000E+00 20.00 0.4758E+02
39.047 0.2325E+02 0.0000E+00 30.00 0.3701E+02
51.485 0.1790E+02 0.0000E+00 40.00 0.2664E+02
63.483 0.1313E+02 0.0000E+00 50.00 0.1803E+02
74.987 0.9572E+01 0.0000E+00 60.00 0.1201E+02
85.971 0.7565E+01 0.0000E+00 70.00 0.8609E+01
96.431 0.7118E+01 0.0000E+00 80.00 0.7326E+01
106.379 0.8017E+01 0.0000E+00 90.00 0.7463E+01
115.846 0.9925E+01 0.0000E+00 100.00 0.8379E+01
124.870 0.1246E+02 0.0000E+00 110.00 0.9596E+01
133.498 0.1528E+02 0.0000E+00 120.00 0.1081E+02
141.781 0.1809E+02 0.0000E+00 130.00 0.1187E+02
149.775 0.2064E+02 0.0000E+00 140.00 0.1272E+02
157.536 0.2277E+02 0.0000E+00 150.00 0.1334E+02
165.121 0.2437E+02 0.0000E+00 160.00 0.1376E+02
172.590 0.2536E+02 0.0000E+00 170.00 0.1399E+02
0.000 0.3240E+02 0.0000E+00 180.00 0.5658E+02
My data is primarily the last two columns.
I have written a script in AWK to strip columns 4 & 5, though i have
not tried to run this data file through my C program, i am fairly
confident that it will not understand the syntan.
#include <stdio.h>
int main(void)
{
char input[] = "0.000 0.3231E+02 0.0000E+00 0.00 0.5642E+02\n"
"13.191 0.3421E+02 0.0000E+00 10.00 0.5910E+02\n"
"26.248 0.2842E+02 0.0000E+00 20.00 0.4758E+02\n"
"39.047 0.2325E+02 0.0000E+00 30.00 0.3701E+02\n"
"51.485 0.1790E+02 0.0000E+00 40.00 0.2664E+02\n"
"63.483 0.1313E+02 0.0000E+00 50.00 0.1803E+02\n"
"74.987 0.9572E+01 0.0000E+00 60.00 0.1201E+02\n"
"85.971 0.7565E+01 0.0000E+00 70.00 0.8609E+01\n"
"96.431 0.7118E+01 0.0000E+00 80.00 0.7326E+01\n"
"106.379 0.8017E+01 0.0000E+00 90.00 0.7463E+01\n"
"115.846 0.9925E+01 0.0000E+00 100.00 0.8379E+01\n"
"124.870 0.1246E+02 0.0000E+00 110.00 0.9596E+01\n"
"133.498 0.1528E+02 0.0000E+00 120.00 0.1081E+02\n"
"141.781 0.1809E+02 0.0000E+00 130.00 0.1187E+02\n"
"149.775 0.2064E+02 0.0000E+00 140.00 0.1272E+02\n"
"157.536 0.2277E+02 0.0000E+00 150.00 0.1334E+02\n"
"165.121 0.2437E+02 0.0000E+00 160.00 0.1376E+02\n"
"172.590 0.2536E+02 0.0000E+00 170.00 0.1399E+02\n"
"0.000 0.3240E+02 0.0000E+00 180.00 0.5658E+02\n", *p;
double x[5];
int n = 0;
printf("The input stream contains:\n%s\n", input);
for (p = input;; p += n) {
if (sscanf(p, "%lg%lg%lg%lg%lg%n",
&x[0], &x[1], &x[2], &x[3], &x[4], &n) != 5)
break;
printf
("After %d more characters, the x array contains\n"
"%g %E %E %g %E\n\n", n, x[0], x[1], x[2], x[3], x[4]);
}
return 0;
}
The input stream contains:
0.000 0.3231E+02 0.0000E+00 0.00 0.5642E+02
13.191 0.3421E+02 0.0000E+00 10.00 0.5910E+02
26.248 0.2842E+02 0.0000E+00 20.00 0.4758E+02
39.047 0.2325E+02 0.0000E+00 30.00 0.3701E+02
51.485 0.1790E+02 0.0000E+00 40.00 0.2664E+02
63.483 0.1313E+02 0.0000E+00 50.00 0.1803E+02
74.987 0.9572E+01 0.0000E+00 60.00 0.1201E+02
85.971 0.7565E+01 0.0000E+00 70.00 0.8609E+01
96.431 0.7118E+01 0.0000E+00 80.00 0.7326E+01
106.379 0.8017E+01 0.0000E+00 90.00 0.7463E+01
115.846 0.9925E+01 0.0000E+00 100.00 0.8379E+01
124.870 0.1246E+02 0.0000E+00 110.00 0.9596E+01
133.498 0.1528E+02 0.0000E+00 120.00 0.1081E+02
141.781 0.1809E+02 0.0000E+00 130.00 0.1187E+02
149.775 0.2064E+02 0.0000E+00 140.00 0.1272E+02
157.536 0.2277E+02 0.0000E+00 150.00 0.1334E+02
165.121 0.2437E+02 0.0000E+00 160.00 0.1376E+02
172.590 0.2536E+02 0.0000E+00 170.00 0.1399E+02
0.000 0.3240E+02 0.0000E+00 180.00 0.5658E+02
After 49 more characters, the x array contains
0 3.231000E+01 0.000000E+00 0 5.642000E+01
After 51 more characters, the x array contains
13.191 3.421000E+01 0.000000E+00 10 5.910000E+01
After 51 more characters, the x array contains
26.248 2.842000E+01 0.000000E+00 20 4.758000E+01
After 51 more characters, the x array contains
39.047 2.325000E+01 0.000000E+00 30 3.701000E+01
After 51 more characters, the x array contains
51.485 1.790000E+01 0.000000E+00 40 2.664000E+01
After 51 more characters, the x array contains
63.483 1.313000E+01 0.000000E+00 50 1.803000E+01
After 51 more characters, the x array contains
74.987 9.572000E+00 0.000000E+00 60 1.201000E+01
After 51 more characters, the x array contains
85.971 7.565000E+00 0.000000E+00 70 8.609000E+00
After 51 more characters, the x array contains
96.431 7.118000E+00 0.000000E+00 80 7.326000E+00
After 52 more characters, the x array contains
106.379 8.017000E+00 0.000000E+00 90 7.463000E+00
After 52 more characters, the x array contains
115.846 9.925000E+00 0.000000E+00 100 8.379000E+00
After 52 more characters, the x array contains
124.87 1.246000E+01 0.000000E+00 110 9.596000E+00
After 52 more characters, the x array contains
133.498 1.528000E+01 0.000000E+00 120 1.081000E+01
After 52 more characters, the x array contains
141.781 1.809000E+01 0.000000E+00 130 1.187000E+01
After 52 more characters, the x array contains
149.775 2.064000E+01 0.000000E+00 140 1.272000E+01
After 52 more characters, the x array contains
157.536 2.277000E+01 0.000000E+00 150 1.334000E+01
After 52 more characters, the x array contains
165.121 2.437000E+01 0.000000E+00 160 1.376000E+01
After 52 more characters, the x array contains
172.59 2.536000E+01 0.000000E+00 170 1.399000E+01
After 50 more characters, the x array contains
0 3.240000E+01 0.000000E+00 180 5.658000E+01