Price fraction: Difference between revisions

→‎{{header|C}}: not clever, but less error prone
(→‎{{header|C}}: not clever, but less error prone)
Line 330:
 
=={{header|C}}==
<lang c>#include <stdlibstdio.h>
#include <time.h>
#include <stdio.h>
 
double table[][2] = {
int main()
{0.06, 0.10}, {0.11, 0.18}, {0.16, 0.26}, {0.21, 0.32},
{0.26, 0.38}, {0.31, 0.44}, {0.36, 0.50}, {0.41, 0.54},
{0.46, 0.58}, {0.51, 0.62}, {0.56, 0.66}, {0.61, 0.70},
{0.66, 0.74}, {0.71, 0.78}, {0.76, 0.82}, {0.81, 0.86},
{0.86, 0.90}, {0.91, 0.94}, {0.96, 0.98}, {1.01, 1.00},
{-1, 0}, /* guarding element */
};
 
double price_fix(double x)
{
int i;
//Just get a random price between 0 and 1
for (i = 0; table[i][0] > 0; i++)
srand(time(NULL));
if (x < table[i][0]) return table[i][1];
float price = (float)((int)rand()%101)/100;
float tops=0.06;
float std_val = 0.10;
 
abort(); /* what else to do? */
//Conditionals are a little odd here
}
//"(price-0.001 < tops && price+0.001 > tops)" is to check if they are equal. Stupid C floats, right? :)
while((price>tops || (price-0.001 < tops && price+0.001 > tops)) && tops<=1.01)
{
tops+=0.05;
 
int main()
if(std_val < 0.26)
{
std_val += 0.08;
int i;
else if(std_val<0.50)
for (i = 0; i <= 100; i++)
std_val +=0.06;
printf("%.2f %.2f\n", i / 100., price_fix(i / 100.));
else
std_val +=0.04;
 
if(std_val > 0.98)
std_val = 1.0;
}
 
return 0;
printf("Value: %.2f\nConverted to standard: %.2f\n", price, std_val);
}</lang>
 
Anonymous user