fork download
  1. #import <objc/objc.h>
  2. #import <objc/Object.h>
  3. #import <math.h>
  4.  
  5. @implementation TestObj
  6.  
  7. typedef struct d1
  8. {
  9. float interval;
  10. float value;
  11.  
  12. int multiplierF;
  13. float floor;
  14.  
  15. int multiplierR;
  16. float round;
  17. } Data;
  18.  
  19. float floorHelper(float value, float roundingValue, int *multiplier);
  20. float roundHelper(float value, float roundingValue, int *multiplier);
  21.  
  22. float floorHelper(float value, float roundingValue, int *multiplier) {
  23. *multiplier = (int)floor(value / roundingValue);
  24. return *multiplier * roundingValue;
  25. }
  26.  
  27. float roundHelper(float value, float roundingValue, int *multiplier)
  28. {
  29. *multiplier = (int)round(value / roundingValue);
  30. return *multiplier * roundingValue;
  31. }
  32.  
  33.  
  34. int main()
  35. {
  36.  
  37.  
  38. float intervals[3] = {0.03, 0.05, 0.08};
  39. float values[3] = {449.263824, 390.928070, 390.878082};
  40.  
  41. int intervalCount = sizeof(intervals)/sizeof(float);
  42. int valueCount = sizeof(values)/sizeof(float);
  43.  
  44. int index = 0;
  45. Data testDataResult[intervalCount * valueCount];
  46.  
  47. // loop thru the intervals
  48. for (int i = 0; i < intervalCount; i++)
  49. {
  50. // loop thru values
  51. for(int x = 0; x < valueCount; x++)
  52. {
  53. testDataResult[index].interval = intervals[i];
  54. testDataResult[index].value = values[x];
  55.  
  56. testDataResult[index].floor = floorHelper(testDataResult[index].value,
  57. testDataResult[index].interval,
  58. &testDataResult[index].multiplierF);
  59. testDataResult[index].round = roundHelper(testDataResult[index].value,
  60. testDataResult[index].interval,
  61. &testDataResult[index].multiplierR);
  62. // printf("%f %f\n", intervals[i], values[x]);
  63. index++;
  64. }
  65. }
  66.  
  67. // printf the test data
  68. //
  69. printf("Interval Value Division Floor Round\n");
  70. for (int y = 0; y < sizeof(testDataResult)/sizeof(Data); y++)
  71. {
  72. printf("%-10.4f %-14f %9.3f %-6d %-10.2f %-6d %-10.2f", testDataResult[y].interval, testDataResult[y].value, testDataResult[y].value / testDataResult[y].interval,
  73. testDataResult[y].multiplierF, testDataResult[y].floor,
  74. testDataResult[y].multiplierR, testDataResult[y].round);
  75.  
  76. // indicate rows where the values equal
  77. if (testDataResult[y].round == testDataResult[y].floor)
  78. {
  79. printf("\n");
  80. }
  81. else
  82. {
  83. printf(" <---- \n");
  84. }
  85.  
  86. if (((y + 1) % intervalCount) == 0)
  87. {
  88. printf("\n");
  89. }
  90. }
  91.  
  92. printf("Done!\n");
  93. return 0;
  94. }
  95. @end
Success #stdin #stdout 0.01s 10128KB
stdin
Standard input is empty
stdout
Interval   Value          Division     Floor             Round
0.0300     449.263824     14975.461    14975  449.25     14975  449.25    
0.0300     390.928070     13030.936    13030  390.90     13031  390.93     <---- 
0.0300     390.878082     13029.270    13029  390.87     13029  390.87    

0.0500     449.263824      8985.276    8985   449.25     8985   449.25    
0.0500     390.928070      7818.561    7818   390.90     7819   390.95     <---- 
0.0500     390.878082      7817.562    7817   390.85     7818   390.90     <---- 

0.0800     449.263824      5615.798    5615   449.20     5616   449.28     <---- 
0.0800     390.928070      4886.601    4886   390.88     4887   390.96     <---- 
0.0800     390.878082      4885.976    4885   390.80     4886   390.88     <---- 

Done!