cae7291

simple text_to_number function

Aug 12th, 2025
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. // A few issues ...
  2. //
  3. // 1. Use of `pow` here isn't warranted -- it adds slowness and imprecision.
  4. //
  5. // 2. `search_first_character_in_text` could be replaced by `strchr`. It is
  6. // called twice. But, could be eliminated.
  7. //
  8. // 3. `char_to_digit` should return `int`. It could also be simplified.
  9. // But it could/should be eliminated.
  10. //
  11. // 4. The `template` has `typename number` but `number` isn't used.
  12. //
  13. // 5. Not really a good candidate for a `template`
  14. //
  15. // 6. No error checking
  16. //
  17. // 7. No real need to pass the string length. A running check for EOS (0x00)
  18. // is sufficient (faster).
  19. //
  20. // Unfortunately, I had to completely restructure your code.
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24.  
  25. double
  26. text_to_number(const char *text,int *erroff)
  27. {
  28. const char *cur = text++;
  29. int chr;
  30. int neg = 0;
  31. int gotdot = 0;
  32. int fractional_div = 1.0;
  33. double converted_number = 0.0;
  34.  
  35. // NOTE: replace the error handling with whatever you'd like (e.g. throw)
  36. *erroff = -1;
  37.  
  38. // assume number is (of the form):
  39. // -34531.742
  40.  
  41. // handle negative sign
  42. chr = *cur;
  43. if (chr == '-') {
  44. neg = 1;
  45. ++cur;
  46. }
  47.  
  48. for (chr = *cur++; chr != 0; chr = *cur++) {
  49. // look for the "."
  50. if (chr == '.') {
  51. // we got: 123.45.6
  52. if (gotdot) {
  53. *erroff = cur - text;
  54. break;
  55. }
  56. gotdot = 1;
  57. continue;
  58. }
  59.  
  60. // valid digit
  61. if ((chr >= '0') && (chr <= '9')) {
  62. // get the value
  63. double dig = chr - '0';
  64.  
  65. // convert fractional part:
  66. // 7 --> 0.7
  67. // 4 --> 0.04
  68. // 2 --> 0.002
  69. if (gotdot) {
  70. fractional_div *= 10.0;
  71. dig /= fractional_div;
  72. }
  73.  
  74. // convert main part
  75. else
  76. converted_number *= 10.0;
  77.  
  78. // add in the digit
  79. converted_number += dig;
  80.  
  81. continue;
  82. }
  83.  
  84. // invalid syntax
  85. *erroff = cur - text;
  86. break;
  87. }
  88.  
  89. // apply sign
  90. if (neg)
  91. converted_number = -converted_number;
  92.  
  93. return converted_number;
  94. }
  95.  
  96. void
  97. test(const char *text)
  98. {
  99. int erroff;
  100. double ret = text_to_number(text,&erroff);
  101.  
  102. printf("'%s' --> %.15g",text,ret);
  103.  
  104. if (erroff >= 0) {
  105. text += erroff;
  106. printf(" ERROR: '%s'",text);
  107. }
  108.  
  109. printf("\n");
  110. }
  111.  
  112. int
  113. main(void)
  114. {
  115.  
  116. test("34531.742");
  117. test("-34531.742");
  118. test("-34531.7426821");
  119. test("-345.31.7426821");
  120. test("-34531.7426-821");
  121.  
  122. return 0;
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment