formatfloat.nim 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2022 Nim contributors
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. when defined(nimPreviewSlimSystem):
  10. import std/assertions
  11. else:
  12. {.deprecated: "formatfloat is about to move out of system; use `-d:nimPreviewSlimSystem` and import `std/formatfloat`".}
  13. proc c_memcpy(a, b: pointer, size: csize_t): pointer {.importc: "memcpy", header: "<string.h>", discardable.}
  14. proc addCstringN(result: var string, buf: cstring; buflen: int) =
  15. # no nimvm support needed, so it doesn't need to be fast here either
  16. let oldLen = result.len
  17. let newLen = oldLen + buflen
  18. result.setLen newLen
  19. c_memcpy(result[oldLen].addr, buf, buflen.csize_t)
  20. import std/private/[dragonbox, schubfach]
  21. proc writeFloatToBufferRoundtrip*(buf: var array[65, char]; value: BiggestFloat): int =
  22. ## This is the implementation to format floats.
  23. ##
  24. ## returns the amount of bytes written to `buf` not counting the
  25. ## terminating '\0' character.
  26. result = toChars(buf, value, forceTrailingDotZero=true)
  27. buf[result] = '\0'
  28. proc writeFloatToBufferRoundtrip*(buf: var array[65, char]; value: float32): int =
  29. result = float32ToChars(buf, value, forceTrailingDotZero=true)
  30. buf[result] = '\0'
  31. proc c_sprintf(buf, frmt: cstring): cint {.header: "<stdio.h>",
  32. importc: "sprintf", varargs, noSideEffect.}
  33. proc writeToBuffer(buf: var array[65, char]; value: cstring) =
  34. var i = 0
  35. while value[i] != '\0':
  36. buf[i] = value[i]
  37. inc i
  38. proc writeFloatToBufferSprintf*(buf: var array[65, char]; value: BiggestFloat): int =
  39. ## This is the implementation to format floats.
  40. ##
  41. ## returns the amount of bytes written to `buf` not counting the
  42. ## terminating '\0' character.
  43. var n: int = c_sprintf(addr buf, "%.16g", value)
  44. var hasDot = false
  45. for i in 0..n-1:
  46. if buf[i] == ',':
  47. buf[i] = '.'
  48. hasDot = true
  49. elif buf[i] in {'a'..'z', 'A'..'Z', '.'}:
  50. hasDot = true
  51. if not hasDot:
  52. buf[n] = '.'
  53. buf[n+1] = '0'
  54. buf[n+2] = '\0'
  55. result = n + 2
  56. else:
  57. result = n
  58. # On Windows nice numbers like '1.#INF', '-1.#INF' or '1.#NAN' or 'nan(ind)'
  59. # of '-1.#IND' are produced.
  60. # We want to get rid of these here:
  61. if buf[n-1] in {'n', 'N', 'D', 'd', ')'}:
  62. writeToBuffer(buf, "nan")
  63. result = 3
  64. elif buf[n-1] == 'F':
  65. if buf[0] == '-':
  66. writeToBuffer(buf, "-inf")
  67. result = 4
  68. else:
  69. writeToBuffer(buf, "inf")
  70. result = 3
  71. proc writeFloatToBuffer*(buf: var array[65, char]; value: BiggestFloat | float32): int {.inline.} =
  72. when defined(nimPreviewFloatRoundtrip) or defined(nimPreviewSlimSystem):
  73. writeFloatToBufferRoundtrip(buf, value)
  74. else:
  75. writeFloatToBufferSprintf(buf, value)
  76. proc addFloatRoundtrip*(result: var string; x: float | float32) =
  77. when nimvm:
  78. doAssert false
  79. else:
  80. var buffer {.noinit.}: array[65, char]
  81. let n = writeFloatToBufferRoundtrip(buffer, x)
  82. result.addCstringN(cstring(buffer[0].addr), n)
  83. proc addFloatSprintf*(result: var string; x: float) =
  84. when nimvm:
  85. doAssert false
  86. else:
  87. var buffer {.noinit.}: array[65, char]
  88. let n = writeFloatToBufferSprintf(buffer, x)
  89. result.addCstringN(cstring(buffer[0].addr), n)
  90. proc nimFloatToString(a: float): cstring =
  91. ## ensures the result doesn't print like an integer, i.e. return 2.0, not 2
  92. # print `-0.0` properly
  93. asm """
  94. function nimOnlyDigitsOrMinus(n) {
  95. return n.toString().match(/^-?\d+$/);
  96. }
  97. if (Number.isSafeInteger(`a`))
  98. `result` = `a` === 0 && 1 / `a` < 0 ? "-0.0" : `a`+".0"
  99. else {
  100. `result` = `a`+""
  101. if(nimOnlyDigitsOrMinus(`result`)){
  102. `result` = `a`+".0"
  103. }
  104. }
  105. """
  106. proc addFloat*(result: var string; x: float | float32) {.inline.} =
  107. ## Converts float to its string representation and appends it to `result`.
  108. runnableExamples:
  109. var
  110. s = "foo:"
  111. b = 45.67
  112. s.addFloat(45.67)
  113. assert s == "foo:45.67"
  114. template impl =
  115. when defined(nimPreviewFloatRoundtrip) or defined(nimPreviewSlimSystem):
  116. addFloatRoundtrip(result, x)
  117. else:
  118. addFloatSprintf(result, x)
  119. when defined(js):
  120. when nimvm: impl()
  121. else:
  122. result.add nimFloatToString(x)
  123. else: impl()
  124. when defined(nimPreviewSlimSystem):
  125. func `$`*(x: float | float32): string =
  126. ## Outplace version of `addFloat`.
  127. result.addFloat(x)