| 1 |
(****************************************************************************** |
| 2 |
* PasDblStrUtils * |
| 3 |
****************************************************************************** |
| 4 |
* Version 2016-09-12-08-18-0000 * |
| 5 |
****************************************************************************** |
| 6 |
* zlib license * |
| 7 |
*============================================================================* |
| 8 |
* * |
| 9 |
* Copyright (C) 2016, Benjamin Rosseaux ([email protected]) * |
| 10 |
* * |
| 11 |
* This software is provided 'as-is', without any express or implied * |
| 12 |
* warranty. In no event will the authors be held liable for any damages * |
| 13 |
* arising from the use of this software. * |
| 14 |
* * |
| 15 |
* Permission is granted to anyone to use this software for any purpose, * |
| 16 |
* including commercial applications, and to alter it and redistribute it * |
| 17 |
* freely, subject to the following restrictions: * |
| 18 |
* * |
| 19 |
* 1. The origin of this software must not be misrepresented; you must not * |
| 20 |
* claim that you wrote the original software. If you use this software * |
| 21 |
* in a product, an acknowledgement in the product documentation would be * |
| 22 |
* appreciated but is not required. * |
| 23 |
* 2. Altered source versions must be plainly marked as such, and must not be * |
| 24 |
* misrepresented as being the original software. * |
| 25 |
* 3. This notice may not be removed or altered from any source distribution. * |
| 26 |
* * |
| 27 |
****************************************************************************** |
| 28 |
* General guidelines for code contributors * |
| 29 |
*============================================================================* |
| 30 |
* * |
| 31 |
* 1. Make sure you are legally allowed to make a contribution under the zlib * |
| 32 |
* license. * |
| 33 |
* 2. The zlib license header goes at the top of each source file, with * |
| 34 |
* appropriate copyright notice. * |
| 35 |
* 3. After a pull request, check the status of your pull request on * |
| 36 |
http://github.com/BeRo1985/pasdblstrutils * |
| 37 |
* 4. Write code, which is compatible with Delphi 7-XE7 and FreePascal >= 3.0 * |
| 38 |
* so don't use generics/templates, operator overloading and another newer * |
| 39 |
* syntax features than Delphi 7 has support for that, but if needed, make * |
| 40 |
* it out-ifdef-able. * |
| 41 |
* 5. Don't use Delphi-only, FreePascal-only or Lazarus-only libraries/units, * |
| 42 |
* but if needed, make it out-ifdef-able. * |
| 43 |
* 6. No use of third-party libraries/units as possible, but if needed, make * |
| 44 |
* it out-ifdef-able. * |
| 45 |
* 7. Try to use const when possible. * |
| 46 |
* 8. Make sure to comment out writeln, used while debugging. * |
| 47 |
* 9. Make sure the code compiles on 32-bit and 64-bit platforms (x86-32, * |
| 48 |
* x86-64, ARM, ARM64, etc.). * |
| 49 |
* 10. Make sure the code runs on platforms with weak and strong memory * |
| 50 |
* models without any issues. * |
| 51 |
* * |
| 52 |
******************************************************************************) |
| 53 |
unit CastlePasDblStrUtils; |
| 54 |
{$ifdef fpc} |
| 55 |
{$mode delphi} |
| 56 |
{$ifdef cpui386} |
| 57 |
{$define cpu386} |
| 58 |
{$endif} |
| 59 |
{$ifdef cpu386} |
| 60 |
{$asmmode intel} |
| 61 |
{$endif} |
| 62 |
{$ifdef cpuamd64} |
| 63 |
{$asmmode intel} |
| 64 |
{$endif} |
| 65 |
{$ifdef FPC_LITTLE_ENDIAN} |
| 66 |
{$define LITTLE_ENDIAN} |
| 67 |
{$else} |
| 68 |
{$ifdef FPC_BIG_ENDIAN} |
| 69 |
{$define BIG_ENDIAN} |
| 70 |
{$endif} |
| 71 |
{$endif} |
| 72 |
{-$pic off} |
| 73 |
{$define CanInline} |
| 74 |
{$ifdef FPC_HAS_TYPE_EXTENDED} |
| 75 |
{$define HAS_TYPE_EXTENDED} |
| 76 |
{$else} |
| 77 |
{$undef HAS_TYPE_EXTENDED} |
| 78 |
{$endif} |
| 79 |
{$ifdef FPC_HAS_TYPE_DOUBLE} |
| 80 |
{$define HAS_TYPE_DOUBLE} |
| 81 |
{$else} |
| 82 |
{$undef HAS_TYPE_DOUBLE} |
| 83 |
{$endif} |
| 84 |
{$ifdef FPC_HAS_TYPE_SINGLE} |
| 85 |
{$define HAS_TYPE_SINGLE} |
| 86 |
{$else} |
| 87 |
{$undef HAS_TYPE_SINGLE} |
| 88 |
{$endif} |
| 89 |
{$if declared(RawByteString)} |
| 90 |
{$define HAS_TYPE_RAWBYTESTRING} |
| 91 |
{$else} |
| 92 |
{$undef HAS_TYPE_RAWBYTESTRING} |
| 93 |
{$ifend} |
| 94 |
{$if declared(UTF8String)} |
| 95 |
{$define HAS_TYPE_UTF8STRING} |
| 96 |
{$else} |
| 97 |
{$undef HAS_TYPE_UTF8STRING} |
| 98 |
{$ifend} |
| 99 |
{$if declared(UnicodeString)} |
| 100 |
{$define HAS_TYPE_UNICODESTRING} |
| 101 |
{$else} |
| 102 |
{$undef HAS_TYPE_UNICODESTRING} |
| 103 |
{$ifend} |
| 104 |
{$else} |
| 105 |
{$realcompatibility off} |
| 106 |
{$localsymbols on} |
| 107 |
{$define LITTLE_ENDIAN} |
| 108 |
{$ifndef cpu64} |
| 109 |
{$define cpu32} |
| 110 |
{$endif} |
| 111 |
{$define HAS_TYPE_EXTENDED} |
| 112 |
{$define HAS_TYPE_DOUBLE} |
| 113 |
{$define HAS_TYPE_SINGLE} |
| 114 |
{$ifdef conditionalexpressions} |
| 115 |
{$if declared(RawByteString)} |
| 116 |
{$define HAS_TYPE_RAWBYTESTRING} |
| 117 |
{$else} |
| 118 |
{$undef HAS_TYPE_RAWBYTESTRING} |
| 119 |
{$ifend} |
| 120 |
{$if declared(UTF8String)} |
| 121 |
{$define HAS_TYPE_UTF8STRING} |
| 122 |
{$else} |
| 123 |
{$undef HAS_TYPE_UTF8STRING} |
| 124 |
{$ifend} |
| 125 |
{$if declared(UnicodeString)} |
| 126 |
{$define HAS_TYPE_UNICODESTRING} |
| 127 |
{$else} |
| 128 |
{$undef HAS_TYPE_UNICODESTRING} |
| 129 |
{$ifend} |
| 130 |
{$else} |
| 131 |
{$undef HAS_TYPE_RAWBYTESTRING} |
| 132 |
{$undef HAS_TYPE_UTF8STRING} |
| 133 |
{$undef HAS_TYPE_UNICODESTRING} |
| 134 |
{$endif} |
| 135 |
{$ifndef BCB} |
| 136 |
{$ifdef ver120} |
| 137 |
{$define Delphi4or5} |
| 138 |
{$endif} |
| 139 |
{$ifdef ver130} |
| 140 |
{$define Delphi4or5} |
| 141 |
{$endif} |
| 142 |
{$ifdef ver140} |
| 143 |
{$define Delphi6} |
| 144 |
{$endif} |
| 145 |
{$ifdef ver150} |
| 146 |
{$define Delphi7} |
| 147 |
{$endif} |
| 148 |
{$ifdef ver170} |
| 149 |
{$define Delphi2005} |
| 150 |
{$endif} |
| 151 |
{$else} |
| 152 |
{$ifdef ver120} |
| 153 |
{$define Delphi4or5} |
| 154 |
{$define BCB4} |
| 155 |
{$endif} |
| 156 |
{$ifdef ver130} |
| 157 |
{$define Delphi4or5} |
| 158 |
{$endif} |
| 159 |
{$endif} |
| 160 |
{$ifdef conditionalexpressions} |
| 161 |
{$if CompilerVersion>=24} |
| 162 |
{$legacyifend on} |
| 163 |
{$ifend} |
| 164 |
{$if CompilerVersion>=14.0} |
| 165 |
{$if CompilerVersion=14.0} |
| 166 |
{$define Delphi6} |
| 167 |
{$ifend} |
| 168 |
{$define Delphi6AndUp} |
| 169 |
{$ifend} |
| 170 |
{$if CompilerVersion>=15.0} |
| 171 |
{$if CompilerVersion=15.0} |
| 172 |
{$define Delphi7} |
| 173 |
{$ifend} |
| 174 |
{$define Delphi7AndUp} |
| 175 |
{$ifend} |
| 176 |
{$if CompilerVersion>=17.0} |
| 177 |
{$if CompilerVersion=17.0} |
| 178 |
{$define Delphi2005} |
| 179 |
{$ifend} |
| 180 |
{$define Delphi2005AndUp} |
| 181 |
{$ifend} |
| 182 |
{$if CompilerVersion>=18.0} |
| 183 |
{$if CompilerVersion=18.0} |
| 184 |
{$define BDS2006} |
| 185 |
{$define Delphi2006} |
| 186 |
{$ifend} |
| 187 |
{$define Delphi2006AndUp} |
| 188 |
{$ifend} |
| 189 |
{$if CompilerVersion>=18.5} |
| 190 |
{$if CompilerVersion=18.5} |
| 191 |
{$define Delphi2007} |
| 192 |
{$ifend} |
| 193 |
{$define Delphi2007AndUp} |
| 194 |
{$ifend} |
| 195 |
{$if CompilerVersion=19.0} |
| 196 |
{$define Delphi2007Net} |
| 197 |
{$ifend} |
| 198 |
{$if CompilerVersion>=20.0} |
| 199 |
{$if CompilerVersion=20.0} |
| 200 |
{$define Delphi2009} |
| 201 |
{$ifend} |
| 202 |
{$define Delphi2009AndUp} |
| 203 |
{$ifend} |
| 204 |
{$if CompilerVersion>=21.0} |
| 205 |
{$if CompilerVersion=21.0} |
| 206 |
{$define Delphi2010} |
| 207 |
{$ifend} |
| 208 |
{$define Delphi2010AndUp} |
| 209 |
{$ifend} |
| 210 |
{$if CompilerVersion>=22.0} |
| 211 |
{$if CompilerVersion=22.0} |
| 212 |
{$define DelphiXE} |
| 213 |
{$ifend} |
| 214 |
{$define DelphiXEAndUp} |
| 215 |
{$ifend} |
| 216 |
{$if CompilerVersion>=23.0} |
| 217 |
{$if CompilerVersion=23.0} |
| 218 |
{$define DelphiXE2} |
| 219 |
{$ifend} |
| 220 |
{$define DelphiXE2AndUp} |
| 221 |
{$ifend} |
| 222 |
{$if CompilerVersion>=24.0} |
| 223 |
{$if CompilerVersion=24.0} |
| 224 |
{$define DelphiXE3} |
| 225 |
{$ifend} |
| 226 |
{$define DelphiXE3AndUp} |
| 227 |
{$ifend} |
| 228 |
{$if CompilerVersion>=25.0} |
| 229 |
{$if CompilerVersion=25.0} |
| 230 |
{$define DelphiXE4} |
| 231 |
{$ifend} |
| 232 |
{$define DelphiXE4AndUp} |
| 233 |
{$ifend} |
| 234 |
{$if CompilerVersion>=26.0} |
| 235 |
{$if CompilerVersion=26.0} |
| 236 |
{$define DelphiXE5} |
| 237 |
{$ifend} |
| 238 |
{$define DelphiXE5AndUp} |
| 239 |
{$ifend} |
| 240 |
{$if CompilerVersion>=27.0} |
| 241 |
{$if CompilerVersion=27.0} |
| 242 |
{$define DelphiXE6} |
| 243 |
{$ifend} |
| 244 |
{$define DelphiXE6AndUp} |
| 245 |
{$ifend} |
| 246 |
{$if CompilerVersion>=28.0} |
| 247 |
{$if CompilerVersion=28.0} |
| 248 |
{$define DelphiXE7} |
| 249 |
{$ifend} |
| 250 |
{$define DelphiXE7AndUp} |
| 251 |
{$ifend} |
| 252 |
{$if CompilerVersion>=29.0} |
| 253 |
{$if CompilerVersion=29.0} |
| 254 |
{$define DelphiXE8} |
| 255 |
{$ifend} |
| 256 |
{$define DelphiXE8AndUp} |
| 257 |
{$ifend} |
| 258 |
{$if CompilerVersion>=30.0} |
| 259 |
{$if CompilerVersion=30.0} |
| 260 |
{$define Delphi10Seattle} |
| 261 |
{$ifend} |
| 262 |
{$define Delphi10SeattleAndUp} |
| 263 |
{$ifend} |
| 264 |
{$if CompilerVersion>=31.0} |
| 265 |
{$if CompilerVersion=31.0} |
| 266 |
{$define Delphi10Berlin} |
| 267 |
{$ifend} |
| 268 |
{$define Delphi10BerlinAndUp} |
| 269 |
{$ifend} |
| 270 |
{$endif} |
| 271 |
{$ifndef Delphi4or5} |
| 272 |
{$ifndef BCB} |
| 273 |
{$define Delphi6AndUp} |
| 274 |
{$endif} |
| 275 |
{$ifndef Delphi6} |
| 276 |
{$define BCB6OrDelphi7AndUp} |
| 277 |
{$ifndef BCB} |
| 278 |
{$define Delphi7AndUp} |
| 279 |
{$endif} |
| 280 |
{$ifndef BCB} |
| 281 |
{$ifndef Delphi7} |
| 282 |
{$ifndef Delphi2005} |
| 283 |
{$define BDS2006AndUp} |
| 284 |
{$endif} |
| 285 |
{$endif} |
| 286 |
{$endif} |
| 287 |
{$endif} |
| 288 |
{$endif} |
| 289 |
{$ifdef Delphi6AndUp} |
| 290 |
{$warn symbol_platform off} |
| 291 |
{$warn symbol_deprecated off} |
| 292 |
{$endif} |
| 293 |
{$endif} |
| 294 |
{$ifdef win32} |
| 295 |
{$define windows} |
| 296 |
{$endif} |
| 297 |
{$ifdef win64} |
| 298 |
{$define windows} |
| 299 |
{$endif} |
| 300 |
{$ifdef wince} |
| 301 |
{$define windows} |
| 302 |
{$endif} |
| 303 |
{$ifndef HAS_TYPE_DOUBLE} |
| 304 |
{$error No double floating point precision} |
| 305 |
{$endif} |
| 306 |
{$rangechecks off} |
| 307 |
{$extendedsyntax on} |
| 308 |
{$writeableconst on} |
| 309 |
{$hints off} |
| 310 |
{$booleval off} |
| 311 |
{$typedaddress off} |
| 312 |
{$stackframes off} |
| 313 |
{$varstringchecks on} |
| 314 |
{$typeinfo on} |
| 315 |
{$overflowchecks off} |
| 316 |
{$longstrings on} |
| 317 |
{$openstrings on} |
| 318 |
|
| 319 |
// CGE: fix for Delphi on non-Windows, also makes sense for WebAssembly |
| 320 |
{$if (not defined(FPC)) and (not defined(MSWINDOWS))} |
| 321 |
{$define USE_TRIVIAL_IMPLEMENTATION} |
| 322 |
{$ifend} |
| 323 |
{$if defined(WASI)} |
| 324 |
{$define USE_TRIVIAL_IMPLEMENTATION} |
| 325 |
{$ifend} |
| 326 |
|
| 327 |
{ CGE: Delphi on Linux doesn't define |
| 328 |
- TFPUPrecisionMode, SetPrecisionMode |
| 329 |
- TFPUExceptionMask, SetFPUExceptionMask } |
| 330 |
{$if defined(FPC) or defined(MSWINDOWS)} |
| 331 |
{$define HAS_FPU_TYPES} |
| 332 |
{$ifend} |
| 333 |
|
| 334 |
interface |
| 335 |
|
| 336 |
uses SysUtils,Math; |
| 337 |
|
| 338 |
type PPasDblStrUtilsInt8=^TPasDblStrUtilsInt8; |
| 339 |
TPasDblStrUtilsInt8={$ifdef fpc}Int8{$else}ShortInt{$endif}; |
| 340 |
|
| 341 |
PPasDblStrUtilsUInt8=^TPasDblStrUtilsUInt8; |
| 342 |
TPasDblStrUtilsUInt8={$ifdef fpc}UInt8{$else}Byte{$endif}; |
| 343 |
|
| 344 |
PPasDblStrUtilsInt16=^TPasDblStrUtilsInt16; |
| 345 |
TPasDblStrUtilsInt16={$ifdef fpc}Int16{$else}SmallInt{$endif}; |
| 346 |
|
| 347 |
PPasDblStrUtilsUInt16=^TPasDblStrUtilsUInt16; |
| 348 |
TPasDblStrUtilsUInt16={$ifdef fpc}UInt16{$else}Word{$endif}; |
| 349 |
|
| 350 |
PPasDblStrUtilsInt32=^TPasDblStrUtilsInt32; |
| 351 |
TPasDblStrUtilsInt32={$ifdef fpc}Int32{$else}Integer{$endif}; |
| 352 |
|
| 353 |
PPasDblStrUtilsUInt32=^TPasDblStrUtilsUInt32; |
| 354 |
TPasDblStrUtilsUInt32={$ifdef fpc}UInt32{$else}Cardinal{$endif}; |
| 355 |
|
| 356 |
PPasDblStrUtilsInt64=^TPasDblStrUtilsInt64; |
| 357 |
TPasDblStrUtilsInt64=Int64; |
| 358 |
|
| 359 |
PPasDblStrUtilsUInt64=^TPasDblStrUtilsUInt64; |
| 360 |
TPasDblStrUtilsUInt64=UInt64; |
| 361 |
|
| 362 |
PPasDblStrUtilsDouble=^TPasDblStrUtilsDouble; |
| 363 |
TPasDblStrUtilsDouble=Double; |
| 364 |
|
| 365 |
PPasDblStrUtilsBoolean=^TPasDblStrUtilsBoolean; |
| 366 |
TPasDblStrUtilsBoolean=Boolean; |
| 367 |
|
| 368 |
PPasDblStrUtilsPtrUInt=^TPasDblStrUtilsPtrUInt; |
| 369 |
PPasDblStrUtilsPtrInt=^TPasDblStrUtilsPtrInt; |
| 370 |
|
| 371 |
{$ifdef fpc} |
| 372 |
TPasDblStrUtilsPtrUInt=PtrUInt; |
| 373 |
TPasDblStrUtilsPtrInt=PtrInt; |
| 374 |
{$else} |
| 375 |
{$if Declared(CompilerVersion) and (CompilerVersion>=23.0)} |
| 376 |
TPasDblStrUtilsPtrUInt=NativeUInt; |
| 377 |
TPasDblStrUtilsPtrInt=NativeInt; |
| 378 |
{$else} |
| 379 |
{$ifdef cpu64} |
| 380 |
TPasDblStrUtilsPtrUInt=TPasDblStrUtilsUInt64; |
| 381 |
TPasDblStrUtilsPtrInt=TPasDblStrUtilsInt64; |
| 382 |
{$else} |
| 383 |
TPasDblStrUtilsPtrUInt=TPasDblStrUtilsUInt32; |
| 384 |
TPasDblStrUtilsPtrInt=TPasDblStrUtilsInt32; |
| 385 |
{$endif} |
| 386 |
{$ifend} |
| 387 |
{$endif} |
| 388 |
|
| 389 |
PPasDblStrUtilsNativeUInt=^TPasDblStrUtilsNativeUInt; |
| 390 |
PPasDblStrUtilsNativeInt=^TPasDblStrUtilsNativeInt; |
| 391 |
TPasDblStrUtilsNativeUInt=TPasDblStrUtilsPtrUInt; |
| 392 |
TPasDblStrUtilsNativeInt=TPasDblStrUtilsPtrInt; |
| 393 |
|
| 394 |
PPasDblStrUtilsRawByteChar=PAnsiChar; |
| 395 |
TPasDblStrUtilsRawByteChar=AnsiChar; |
| 396 |
|
| 397 |
PPasDblStrUtilsRawByteCharSet=^TPasDblStrUtilsRawByteCharSet; |
| 398 |
TPasDblStrUtilsRawByteCharSet=set of TPasDblStrUtilsRawByteChar; |
| 399 |
|
| 400 |
PPasDblStrUtilsRawByteString=^TPasDblStrUtilsRawByteString; |
| 401 |
TPasDblStrUtilsRawByteString={$ifdef HAS_TYPE_RAWBYTESTRING}RawByteString{$else}AnsiString{$endif}; |
| 402 |
|
| 403 |
PPasDblStrUtilsUTF8Char=PAnsiChar; |
| 404 |
TPasDblStrUtilsUTF8Char=AnsiChar; |
| 405 |
|
| 406 |
PPasDblStrUtilsUTF8String=^TPasDblStrUtilsUTF8String; |
| 407 |
TPasDblStrUtilsUTF8String={$ifdef HAS_TYPE_UTF8STRING}UTF8String{$else}AnsiString{$endif}; |
| 408 |
|
| 409 |
PPasDblStrUtilsUTF16Char={$ifdef HAS_TYPE_UNICODESTRING}{$ifdef fpc}PUnicodeChar{$else}PWideChar{$endif}{$else}PWideChar{$endif}; |
| 410 |
TPasDblStrUtilsUTF16Char={$ifdef HAS_TYPE_UNICODESTRING}{$ifdef fpc}UnicodeChar{$else}WideChar{$endif}{$else}WideChar{$endif}; |
| 411 |
|
| 412 |
PPasDblStrUtilsUTF16String=^TPasDblStrUtilsUTF16String; |
| 413 |
TPasDblStrUtilsUTF16String={$ifdef HAS_TYPE_UNICODESTRING}UnicodeString{$else}WideString{$endif}; |
| 414 |
|
| 415 |
PPasDblStrUtilsChar=PAnsiChar; |
| 416 |
TPasDblStrUtilsChar=AnsiChar; |
| 417 |
|
| 418 |
PPasDblStrUtilsString=^TPasDblStrUtilsString; |
| 419 |
TPasDblStrUtilsString={$ifdef HAS_TYPE_RAWBYTESTRING}RawByteString{$else}AnsiString{$endif}; |
| 420 |
|
| 421 |
PPasDblStrUtilsPointer=^TPasDblStrUtilsPointer; |
| 422 |
TPasDblStrUtilsPointer=Pointer; |
| 423 |
|
| 424 |
PPasDblStrUtilsRoundingMode=^TPasDblStrUtilsRoundingMode; |
| 425 |
TPasDblStrUtilsRoundingMode= |
| 426 |
{$ifdef HAS_FPU_TYPES} |
| 427 |
type TFPURoundingMode |
| 428 |
{$else} |
| 429 |
(rmNearest, rmDown, rmUp, rmTruncate) |
| 430 |
{$endif}; |
| 431 |
|
| 432 |
TPasDblStrUtilsOutputMode=( |
| 433 |
omStandard, |
| 434 |
omStandardExponential, |
| 435 |
omFixed, |
| 436 |
omExponential, |
| 437 |
omPrecision, |
| 438 |
omRadix |
| 439 |
); |
| 440 |
|
| 441 |
function ConvertStringToDouble(const StringValue:TPasDblStrUtilsString;const RoundingMode:TPasDblStrUtilsRoundingMode=rmNearest;const OK:PPasDblStrUtilsBoolean=nil;const Base:TPasDblStrUtilsInt32=-1):TPasDblStrUtilsDouble; |
| 442 |
|
| 443 |
function ConvertDoubleToString(const AValue:TPasDblStrUtilsDouble;const OutputMode:TPasDblStrUtilsOutputMode=omStandard;RequestedDigits:TPasDblStrUtilsInt32=-1):TPasDblStrUtilsString; |
| 444 |
|
| 445 |
implementation |
| 446 |
|
| 447 |
{$ifdef USE_TRIVIAL_IMPLEMENTATION} |
| 448 |
{$warn IMPLICIT_STRING_CAST off} |
| 449 |
{$warn IMPLICIT_STRING_CAST_LOSS off} |
| 450 |
|
| 451 |
uses CastleUtils; |
| 452 |
|
| 453 |
function ConvertStringToDouble(const StringValue:TPasDblStrUtilsString;const RoundingMode:TPasDblStrUtilsRoundingMode=rmNearest;const OK:PPasDblStrUtilsBoolean=nil;const Base:TPasDblStrUtilsInt32=-1):TPasDblStrUtilsDouble; |
| 454 |
var |
| 455 |
Success: Boolean; |
| 456 |
begin |
| 457 |
Success := TryStrToFloatDot(StringValue, Result); |
| 458 |
{ Assign OK^, if OK non-nil. } |
| 459 |
if OK <> nil then |
| 460 |
OK^ := Success; |
| 461 |
end; |
| 462 |
|
| 463 |
function ConvertDoubleToString(const AValue:TPasDblStrUtilsDouble;const OutputMode:TPasDblStrUtilsOutputMode=omStandard;RequestedDigits:TPasDblStrUtilsInt32=-1):TPasDblStrUtilsString; |
| 464 |
begin |
| 465 |
Result := FloatToStrDot(AValue); |
| 466 |
end; |
| 467 |
|
| 468 |
{$else} |
| 469 |
|
| 470 |
type PDoubleHiLo=^TDoubleHiLo; |
| 471 |
TDoubleHiLo=packed record |
| 472 |
{$ifdef BIG_ENDIAN} |
| 473 |
Hi,Lo:TPasDblStrUtilsUInt32; |
| 474 |
{$else} |
| 475 |
Lo,Hi:TPasDblStrUtilsUInt32; |
| 476 |
{$endif} |
| 477 |
end; |
| 478 |
|
| 479 |
PDoubleBytes=^TDoubleBytes; |
| 480 |
TDoubleBytes=array[0..sizeof(TPasDblStrUtilsDouble)-1] of TPasDblStrUtilsUInt8; |
| 481 |
|
| 482 |
{$ifdef cpu64} |
| 483 |
function IsNaN(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 484 |
begin |
| 485 |
result:=((PPasDblStrUtilsInt64(@AValue)^ and $7ff0000000000000)=$7ff0000000000000) and ((PPasDblStrUtilsInt64(@AValue)^ and $000fffffffffffff)<>$0000000000000000); |
| 486 |
end; |
| 487 |
|
| 488 |
function IsInfinite(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 489 |
begin |
| 490 |
result:=(PPasDblStrUtilsInt64(@AValue)^ and $7fffffffffffffff)=$7ff0000000000000; |
| 491 |
end; |
| 492 |
|
| 493 |
function IsFinite(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 494 |
begin |
| 495 |
result:=(PPasDblStrUtilsInt64(@AValue)^ and $7ff0000000000000)<>$7ff0000000000000; |
| 496 |
end; |
| 497 |
|
| 498 |
function IsPosInfinite(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 499 |
begin |
| 500 |
result:=PPasDblStrUtilsInt64(@AValue)^=TPasDblStrUtilsInt64($7ff0000000000000); |
| 501 |
end; |
| 502 |
|
| 503 |
function IsNegInfinite(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 504 |
begin |
| 505 |
{$ifdef fpc} |
| 506 |
result:=TPasDblStrUtilsUInt64(pointer(@AValue)^)=TPasDblStrUtilsUInt64($fff0000000000000); |
| 507 |
{$else} |
| 508 |
result:=PPasDblStrUtilsInt64(@AValue)^=TPasDblStrUtilsInt64($fff0000000000000); |
| 509 |
{$endif} |
| 510 |
end; |
| 511 |
|
| 512 |
function IsPosZero(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 513 |
begin |
| 514 |
result:=PPasDblStrUtilsInt64(@AValue)^=TPasDblStrUtilsInt64($0000000000000000); |
| 515 |
end; |
| 516 |
|
| 517 |
function IsNegZero(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 518 |
begin |
| 519 |
{$ifdef fpc} |
| 520 |
result:=TPasDblStrUtilsUInt64(pointer(@AValue)^)=TPasDblStrUtilsUInt64($8000000000000000); |
| 521 |
{$else} |
| 522 |
result:=PPasDblStrUtilsInt64(@AValue)^=TPasDblStrUtilsInt64($8000000000000000); |
| 523 |
{$endif} |
| 524 |
end; |
| 525 |
|
| 526 |
function IsZero(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 527 |
begin |
| 528 |
{$ifdef fpc} |
| 529 |
result:=(TPasDblStrUtilsUInt64(pointer(@AValue)^) and TPasDblStrUtilsUInt64($7fffffffffffffff))=TPasDblStrUtilsUInt64($0000000000000000); |
| 530 |
{$else} |
| 531 |
result:=(PPasDblStrUtilsInt64(@AValue)^ and TPasDblStrUtilsInt64($7fffffffffffffff))=TPasDblStrUtilsInt64($0000000000000000); |
| 532 |
{$endif} |
| 533 |
end; |
| 534 |
|
| 535 |
function IsNegative(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 536 |
begin |
| 537 |
{$ifdef fpc} |
| 538 |
result:=(TPasDblStrUtilsUInt64(pointer(@AValue)^) and TPasDblStrUtilsUInt64($8000000000000000))<>0; |
| 539 |
{$else} |
| 540 |
result:=(PPasDblStrUtilsInt64(@AValue)^ shr 63)<>0; |
| 541 |
{$endif} |
| 542 |
end; |
| 543 |
{$else} |
| 544 |
{$ifdef TrickyNumberChecks} |
| 545 |
function IsNaN(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 546 |
var l:TPasDblStrUtilsUInt32; |
| 547 |
begin |
| 548 |
l:=PDoubleHiLo(@AValue)^.Lo; |
| 549 |
result:=(TPasDblStrUtilsUInt32($7ff00000-TPasDblStrUtilsUInt32(TPasDblStrUtilsUInt32(PDoubleHiLo(@AValue)^.Hi and $7fffffff) or ((l or (-l)) shr 31))) shr 31)<>0; |
| 550 |
end; |
| 551 |
|
| 552 |
function IsInfinite(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 553 |
begin |
| 554 |
result:=TPasDblStrUtilsUInt32((TPasDblStrUtilsUInt32(PDoubleHiLo(@AValue)^.Hi and $7fffffff) xor $7ff00000) or PDoubleHiLo(@AValue)^.Lo)=0; |
| 555 |
end; |
| 556 |
|
| 557 |
function IsFinite(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 558 |
begin |
| 559 |
result:=(TPasDblStrUtilsUInt32((PDoubleHiLo(@AValue)^.Hi and $7fffffff)-$7ff00000) shr 31)<>0; |
| 560 |
end; |
| 561 |
|
| 562 |
function IsPosInfinite(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 563 |
var h:TPasDblStrUtilsUInt32; |
| 564 |
begin |
| 565 |
h:=PDoubleHiLo(@AValue)^.Hi; |
| 566 |
result:=TPasDblStrUtilsUInt32(((TPasDblStrUtilsUInt32(h and $7fffffff) xor $7ff00000) or PDoubleHiLo(@AValue)^.Lo) or TPasDblStrUtilsUInt32(h shr 31))=0; |
| 567 |
end; |
| 568 |
|
| 569 |
function IsNegInfinite(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 570 |
var h:TPasDblStrUtilsUInt32; |
| 571 |
begin |
| 572 |
h:=PDoubleHiLo(@AValue)^.Hi; |
| 573 |
result:=TPasDblStrUtilsUInt32(((TPasDblStrUtilsUInt32(h and $7fffffff) xor $7ff00000) or PDoubleHiLo(@AValue)^.Lo) or TPasDblStrUtilsUInt32(TPasDblStrUtilsUInt32(not h) shr 31))=0; |
| 574 |
end; |
| 575 |
|
| 576 |
function IsPosZero(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 577 |
var h:TPasDblStrUtilsUInt32; |
| 578 |
begin |
| 579 |
h:=PDoubleHiLo(@AValue)^.Hi; |
| 580 |
result:=TPasDblStrUtilsUInt32(TPasDblStrUtilsUInt32(TPasDblStrUtilsUInt32(h and $7fffffff) or PDoubleHiLo(@AValue)^.Lo) or TPasDblStrUtilsUInt32(h shr 31))=0; |
| 581 |
end; |
| 582 |
|
| 583 |
function IsNegZero(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 584 |
var h:TPasDblStrUtilsUInt32; |
| 585 |
begin |
| 586 |
h:=PDoubleHiLo(@AValue)^.Hi; |
| 587 |
result:=TPasDblStrUtilsUInt32(TPasDblStrUtilsUInt32(TPasDblStrUtilsUInt32(h and $7fffffff) or PDoubleHiLo(@AValue)^.Lo) or TPasDblStrUtilsUInt32(TPasDblStrUtilsUInt32(not h) shr 31))=0; |
| 588 |
end; |
| 589 |
|
| 590 |
function IsZero(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 591 |
begin |
| 592 |
result:=TPasDblStrUtilsUInt32(TPasDblStrUtilsUInt32(PDoubleHiLo(@AValue)^.Hi and $7fffffff) or PDoubleHiLo(@AValue)^.Lo)=0; |
| 593 |
end; |
| 594 |
|
| 595 |
function IsNegative(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 596 |
begin |
| 597 |
result:=TPasDblStrUtilsUInt32(PDoubleHiLo(@AValue)^.Hi and TPasDblStrUtilsUInt32($80000000))<>0; |
| 598 |
end; |
| 599 |
{$else} |
| 600 |
function IsNaN(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 601 |
begin |
| 602 |
result:=((PDoubleHiLo(@AValue)^.Hi and $7ff00000)=$7ff00000) and (((PDoubleHiLo(@AValue)^.Hi and $000fffff) or PDoubleHiLo(@AValue)^.Lo)<>0); |
| 603 |
end; |
| 604 |
|
| 605 |
function IsInfinite(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 606 |
begin |
| 607 |
result:=((PDoubleHiLo(@AValue)^.Hi and $7fffffff)=$7ff00000) and (PDoubleHiLo(@AValue)^.Lo=0); |
| 608 |
end; |
| 609 |
|
| 610 |
function IsFinite(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 611 |
begin |
| 612 |
result:=(PDoubleHiLo(@AValue)^.Hi and $7ff00000)<>$7ff00000; |
| 613 |
end; |
| 614 |
|
| 615 |
function IsPosInfinite(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 616 |
begin |
| 617 |
result:=(PDoubleHiLo(@AValue)^.Hi=$7ff00000) and (PDoubleHiLo(@AValue)^.Lo=0); |
| 618 |
end; |
| 619 |
|
| 620 |
function IsNegInfinite(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 621 |
begin |
| 622 |
result:=(PDoubleHiLo(@AValue)^.Hi=$fff00000) and (PDoubleHiLo(@AValue)^.Lo=0); |
| 623 |
end; |
| 624 |
|
| 625 |
function IsPosZero(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 626 |
begin |
| 627 |
result:=(PDoubleHiLo(@AValue)^.Hi or PDoubleHiLo(@AValue)^.Lo)=0; |
| 628 |
end; |
| 629 |
|
| 630 |
function IsNegZero(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 631 |
begin |
| 632 |
result:=(PDoubleHiLo(@AValue)^.Hi=$80000000) and (PDoubleHiLo(@AValue)^.Lo=0); |
| 633 |
end; |
| 634 |
|
| 635 |
function IsZero(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 636 |
begin |
| 637 |
result:=((PDoubleHiLo(@AValue)^.Hi and $7fffffff) or PDoubleHiLo(@AValue)^.Lo)=0; |
| 638 |
end; |
| 639 |
|
| 640 |
function IsNegative(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsBoolean; {$ifdef caninline}inline;{$endif} |
| 641 |
begin |
| 642 |
result:=(PDoubleHiLo(@AValue)^.Hi and $80000000)<>0; |
| 643 |
end; |
| 644 |
{$endif} |
| 645 |
{$endif} |
| 646 |
|
| 647 |
function DoubleAbsolute(const AValue:TPasDblStrUtilsDouble):TPasDblStrUtilsDouble; {$ifdef caninline}inline;{$endif} |
| 648 |
begin |
| 649 |
{$ifdef cpu64} |
| 650 |
PPasDblStrUtilsInt64(@result)^:=PPasDblStrUtilsInt64(@AValue)^ and $7fffffffffffffff; |
| 651 |
{$else} |
| 652 |
PDoubleHiLo(@result)^.Hi:=PDoubleHiLo(@AValue)^.Hi and $7fffffff; |
| 653 |
PDoubleHiLo(@result)^.Lo:=PDoubleHiLo(@AValue)^.Lo; |
| 654 |
{$endif} |
| 655 |
end; |
| 656 |
|
| 657 |
const DoubleToStringPowerOfTenTable:array[0..86,0..2] of TPasDblStrUtilsInt64=((TPasDblStrUtilsInt64($fa8fd5a0081c0288),-1220,-348), |
| 658 |
(TPasDblStrUtilsInt64($baaee17fa23ebf76),-1193,-340), |
| 659 |
(TPasDblStrUtilsInt64($8b16fb203055ac76),-1166,-332), |
| 660 |
(TPasDblStrUtilsInt64($cf42894a5dce35ea),-1140,-324), |
| 661 |
(TPasDblStrUtilsInt64($9a6bb0aa55653b2d),-1113,-316), |
| 662 |
(TPasDblStrUtilsInt64($e61acf033d1a45df),-1087,-308), |
| 663 |
(TPasDblStrUtilsInt64($ab70fe17c79ac6ca),-1060,-300), |
| 664 |
(TPasDblStrUtilsInt64($ff77b1fcbebcdc4f),-1034,-292), |
| 665 |
(TPasDblStrUtilsInt64($be5691ef416bd60c),-1007,-284), |
| 666 |
(TPasDblStrUtilsInt64($8dd01fad907ffc3c),-980,-276), |
| 667 |
(TPasDblStrUtilsInt64($d3515c2831559a83),-954,-268), |
| 668 |
(TPasDblStrUtilsInt64($9d71ac8fada6c9b5),-927,-260), |
| 669 |
(TPasDblStrUtilsInt64($ea9c227723ee8bcb),-901,-252), |
| 670 |
(TPasDblStrUtilsInt64($aecc49914078536d),-874,-244), |
| 671 |
(TPasDblStrUtilsInt64($823c12795db6ce57),-847,-236), |
| 672 |
(TPasDblStrUtilsInt64($c21094364dfb5637),-821,-228), |
| 673 |
(TPasDblStrUtilsInt64($9096ea6f3848984f),-794,-220), |
| 674 |
(TPasDblStrUtilsInt64($d77485cb25823ac7),-768,-212), |
| 675 |
(TPasDblStrUtilsInt64($a086cfcd97bf97f4),-741,-204), |
| 676 |
(TPasDblStrUtilsInt64($ef340a98172aace5),-715,-196), |
| 677 |
(TPasDblStrUtilsInt64($b23867fb2a35b28e),-688,-188), |
| 678 |
(TPasDblStrUtilsInt64($84c8d4dfd2c63f3b),-661,-180), |
| 679 |
(TPasDblStrUtilsInt64($c5dd44271ad3cdba),-635,-172), |
| 680 |
(TPasDblStrUtilsInt64($936b9fcebb25c996),-608,-164), |
| 681 |
(TPasDblStrUtilsInt64($dbac6c247d62a584),-582,-156), |
| 682 |
(TPasDblStrUtilsInt64($a3ab66580d5fdaf6),-555,-148), |
| 683 |
(TPasDblStrUtilsInt64($f3e2f893dec3f126),-529,-140), |
| 684 |
(TPasDblStrUtilsInt64($b5b5ada8aaff80b8),-502,-132), |
| 685 |
(TPasDblStrUtilsInt64($87625f056c7c4a8b),-475,-124), |
| 686 |
(TPasDblStrUtilsInt64($c9bcff6034c13053),-449,-116), |
| 687 |
(TPasDblStrUtilsInt64($964e858c91ba2655),-422,-108), |
| 688 |
(TPasDblStrUtilsInt64($dff9772470297ebd),-396,-100), |
| 689 |
(TPasDblStrUtilsInt64($a6dfbd9fb8e5b88f),-369,-92), |
| 690 |
(TPasDblStrUtilsInt64($f8a95fcf88747d94),-343,-84), |
| 691 |
(TPasDblStrUtilsInt64($b94470938fa89bcf),-316,-76), |
| 692 |
(TPasDblStrUtilsInt64($8a08f0f8bf0f156b),-289,-68), |
| 693 |
(TPasDblStrUtilsInt64($cdb02555653131b6),-263,-60), |
| 694 |
(TPasDblStrUtilsInt64($993fe2c6d07b7fac),-236,-52), |
| 695 |
(TPasDblStrUtilsInt64($e45c10c42a2b3b06),-210,-44), |
| 696 |
(TPasDblStrUtilsInt64($aa242499697392d3),-183,-36), |
| 697 |
(TPasDblStrUtilsInt64($fd87b5f28300ca0e),-157,-28), |
| 698 |
(TPasDblStrUtilsInt64($bce5086492111aeb),-130,-20), |
| 699 |
(TPasDblStrUtilsInt64($8cbccc096f5088cc),-103,-12), |
| 700 |
(TPasDblStrUtilsInt64($d1b71758e219652c),-77,-4), |
| 701 |
(TPasDblStrUtilsInt64($9c40000000000000),-50,4), |
| 702 |
(TPasDblStrUtilsInt64($e8d4a51000000000),-24,12), |
| 703 |
(TPasDblStrUtilsInt64($ad78ebc5ac620000),3,20), |
| 704 |
(TPasDblStrUtilsInt64($813f3978f8940984),30,28), |
| 705 |
(TPasDblStrUtilsInt64($c097ce7bc90715b3),56,36), |
| 706 |
(TPasDblStrUtilsInt64($8f7e32ce7bea5c70),83,44), |
| 707 |
(TPasDblStrUtilsInt64($d5d238a4abe98068),109,52), |
| 708 |
(TPasDblStrUtilsInt64($9f4f2726179a2245),136,60), |
| 709 |
(TPasDblStrUtilsInt64($ed63a231d4c4fb27),162,68), |
| 710 |
(TPasDblStrUtilsInt64($b0de65388cc8ada8),189,76), |
| 711 |
(TPasDblStrUtilsInt64($83c7088e1aab65db),216,84), |
| 712 |
(TPasDblStrUtilsInt64($c45d1df942711d9a),242,92), |
| 713 |
(TPasDblStrUtilsInt64($924d692ca61be758),269,100), |
| 714 |
(TPasDblStrUtilsInt64($da01ee641a708dea),295,108), |
| 715 |
(TPasDblStrUtilsInt64($a26da3999aef774a),322,116), |
| 716 |
(TPasDblStrUtilsInt64($f209787bb47d6b85),348,124), |
| 717 |
(TPasDblStrUtilsInt64($b454e4a179dd1877),375,132), |
| 718 |
(TPasDblStrUtilsInt64($865b86925b9bc5c2),402,140), |
| 719 |
(TPasDblStrUtilsInt64($c83553c5c8965d3d),428,148), |
| 720 |
(TPasDblStrUtilsInt64($952ab45cfa97a0b3),455,156), |
| 721 |
(TPasDblStrUtilsInt64($de469fbd99a05fe3),481,164), |
| 722 |
(TPasDblStrUtilsInt64($a59bc234db398c25),508,172), |
| 723 |
(TPasDblStrUtilsInt64($f6c69a72a3989f5c),534,180), |
| 724 |
(TPasDblStrUtilsInt64($b7dcbf5354e9bece),561,188), |
| 725 |
(TPasDblStrUtilsInt64($88fcf317f22241e2),588,196), |
| 726 |
(TPasDblStrUtilsInt64($cc20ce9bd35c78a5),614,204), |
| 727 |
(TPasDblStrUtilsInt64($98165af37b2153df),641,212), |
| 728 |
(TPasDblStrUtilsInt64($e2a0b5dc971f303a),667,220), |
| 729 |
(TPasDblStrUtilsInt64($a8d9d1535ce3b396),694,228), |
| 730 |
(TPasDblStrUtilsInt64($fb9b7cd9a4a7443c),720,236), |
| 731 |
(TPasDblStrUtilsInt64($bb764c4ca7a44410),747,244), |
| 732 |
(TPasDblStrUtilsInt64($8bab8eefb6409c1a),774,252), |
| 733 |
(TPasDblStrUtilsInt64($d01fef10a657842c),800,260), |
| 734 |
(TPasDblStrUtilsInt64($9b10a4e5e9913129),827,268), |
| 735 |
(TPasDblStrUtilsInt64($e7109bfba19c0c9d),853,276), |
| 736 |
(TPasDblStrUtilsInt64($ac2820d9623bf429),880,284), |
| 737 |
(TPasDblStrUtilsInt64($80444b5e7aa7cf85),907,292), |
| 738 |
(TPasDblStrUtilsInt64($bf21e44003acdd2d),933,300), |
| 739 |
(TPasDblStrUtilsInt64($8e679c2f5e44ff8f),960,308), |
| 740 |
(TPasDblStrUtilsInt64($d433179d9c8cb841),986,316), |
| 741 |
(TPasDblStrUtilsInt64($9e19db92b4e31ba9),1013,324), |
| 742 |
(TPasDblStrUtilsInt64($eb96bf6ebadf77d9),1039,332), |
| 743 |
(TPasDblStrUtilsInt64($af87023b9bf0ee6b),1066,340)); |
| 744 |
|
| 745 |
DoubleToStringPowerOfTenBinaryExponentTable:array[-1220..(1066+27)-1] of TPasDblStrUtilsUInt8=(0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 746 |
1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2, |
| 747 |
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, |
| 748 |
2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3, |
| 749 |
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, |
| 750 |
3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, |
| 751 |
4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5, |
| 752 |
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, |
| 753 |
5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6, |
| 754 |
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, |
| 755 |
6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
| 756 |
7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8, |
| 757 |
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
| 758 |
8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9, |
| 759 |
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, |
| 760 |
9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, |
| 761 |
10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11, |
| 762 |
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11, |
| 763 |
11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,12, |
| 764 |
12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12, |
| 765 |
13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13, |
| 766 |
13,13,13,13,13,13,13,13,13,13,13,14,14,14,14,14, |
| 767 |
14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14, |
| 768 |
14,14,14,14,14,14,15,15,15,15,15,15,15,15,15,15, |
| 769 |
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15, |
| 770 |
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16, |
| 771 |
16,16,16,16,16,16,16,16,16,16,16,17,17,17,17,17, |
| 772 |
17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17, |
| 773 |
17,17,17,17,17,18,18,18,18,18,18,18,18,18,18,18, |
| 774 |
18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18, |
| 775 |
19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19, |
| 776 |
19,19,19,19,19,19,19,19,19,19,20,20,20,20,20,20, |
| 777 |
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20, |
| 778 |
20,20,20,20,20,21,21,21,21,21,21,21,21,21,21,21, |
| 779 |
21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21, |
| 780 |
22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 781 |
22,22,22,22,22,22,22,22,22,22,23,23,23,23,23,23, |
| 782 |
23,23,23,23,23,23,23,23,23,23,23,23,23,23,23,23, |
| 783 |
23,23,23,23,23,24,24,24,24,24,24,24,24,24,24,24, |
| 784 |
24,24,24,24,24,24,24,24,24,24,24,24,24,24,24,25, |
| 785 |
25,25,25,25,25,25,25,25,25,25,25,25,25,25,25,25, |
| 786 |
25,25,25,25,25,25,25,25,25,25,26,26,26,26,26,26, |
| 787 |
26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26, |
| 788 |
26,26,26,26,27,27,27,27,27,27,27,27,27,27,27,27, |
| 789 |
27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,28, |
| 790 |
28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28, |
| 791 |
28,28,28,28,28,28,28,28,28,28,29,29,29,29,29,29, |
| 792 |
29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29, |
| 793 |
29,29,29,29,30,30,30,30,30,30,30,30,30,30,30,30, |
| 794 |
30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,31, |
| 795 |
31,31,31,31,31,31,31,31,31,31,31,31,31,31,31,31, |
| 796 |
31,31,31,31,31,31,31,31,31,32,32,32,32,32,32,32, |
| 797 |
32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32, |
| 798 |
32,32,32,32,33,33,33,33,33,33,33,33,33,33,33,33, |
| 799 |
33,33,33,33,33,33,33,33,33,33,33,33,33,33,34,34, |
| 800 |
34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34, |
| 801 |
34,34,34,34,34,34,34,34,34,35,35,35,35,35,35,35, |
| 802 |
35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35, |
| 803 |
35,35,35,35,36,36,36,36,36,36,36,36,36,36,36,36, |
| 804 |
36,36,36,36,36,36,36,36,36,36,36,36,36,36,37,37, |
| 805 |
37,37,37,37,37,37,37,37,37,37,37,37,37,37,37,37, |
| 806 |
37,37,37,37,37,37,37,37,37,38,38,38,38,38,38,38, |
| 807 |
38,38,38,38,38,38,38,38,38,38,38,38,38,38,38,38, |
| 808 |
38,38,38,39,39,39,39,39,39,39,39,39,39,39,39,39, |
| 809 |
39,39,39,39,39,39,39,39,39,39,39,39,39,39,40,40, |
| 810 |
40,40,40,40,40,40,40,40,40,40,40,40,40,40,40,40, |
| 811 |
40,40,40,40,40,40,40,40,41,41,41,41,41,41,41,41, |
| 812 |
41,41,41,41,41,41,41,41,41,41,41,41,41,41,41,41, |
| 813 |
41,41,41,42,42,42,42,42,42,42,42,42,42,42,42,42, |
| 814 |
42,42,42,42,42,42,42,42,42,42,42,42,42,42,43,43, |
| 815 |
43,43,43,43,43,43,43,43,43,43,43,43,43,43,43,43, |
| 816 |
43,43,43,43,43,43,43,43,44,44,44,44,44,44,44,44, |
| 817 |
44,44,44,44,44,44,44,44,44,44,44,44,44,44,44,44, |
| 818 |
44,44,44,45,45,45,45,45,45,45,45,45,45,45,45,45, |
| 819 |
45,45,45,45,45,45,45,45,45,45,45,45,45,46,46,46, |
| 820 |
46,46,46,46,46,46,46,46,46,46,46,46,46,46,46,46, |
| 821 |
46,46,46,46,46,46,46,46,47,47,47,47,47,47,47,47, |
| 822 |
47,47,47,47,47,47,47,47,47,47,47,47,47,47,47,47, |
| 823 |
47,47,47,48,48,48,48,48,48,48,48,48,48,48,48,48, |
| 824 |
48,48,48,48,48,48,48,48,48,48,48,48,48,49,49,49, |
| 825 |
49,49,49,49,49,49,49,49,49,49,49,49,49,49,49,49, |
| 826 |
49,49,49,49,49,49,49,49,50,50,50,50,50,50,50,50, |
| 827 |
50,50,50,50,50,50,50,50,50,50,50,50,50,50,50,50, |
| 828 |
50,50,51,51,51,51,51,51,51,51,51,51,51,51,51,51, |
| 829 |
51,51,51,51,51,51,51,51,51,51,51,51,51,52,52,52, |
| 830 |
52,52,52,52,52,52,52,52,52,52,52,52,52,52,52,52, |
| 831 |
52,52,52,52,52,52,52,53,53,53,53,53,53,53,53,53, |
| 832 |
53,53,53,53,53,53,53,53,53,53,53,53,53,53,53,53, |
| 833 |
53,53,54,54,54,54,54,54,54,54,54,54,54,54,54,54, |
| 834 |
54,54,54,54,54,54,54,54,54,54,54,54,54,55,55,55, |
| 835 |
55,55,55,55,55,55,55,55,55,55,55,55,55,55,55,55, |
| 836 |
55,55,55,55,55,55,55,56,56,56,56,56,56,56,56,56, |
| 837 |
56,56,56,56,56,56,56,56,56,56,56,56,56,56,56,56, |
| 838 |
56,56,57,57,57,57,57,57,57,57,57,57,57,57,57,57, |
| 839 |
57,57,57,57,57,57,57,57,57,57,57,57,58,58,58,58, |
| 840 |
58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58, |
| 841 |
58,58,58,58,58,58,58,59,59,59,59,59,59,59,59,59, |
| 842 |
59,59,59,59,59,59,59,59,59,59,59,59,59,59,59,59, |
| 843 |
59,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60, |
| 844 |
60,60,60,60,60,60,60,60,60,60,60,60,61,61,61,61, |
| 845 |
61,61,61,61,61,61,61,61,61,61,61,61,61,61,61,61, |
| 846 |
61,61,61,61,61,61,61,62,62,62,62,62,62,62,62,62, |
| 847 |
62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62, |
| 848 |
62,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63, |
| 849 |
63,63,63,63,63,63,63,63,63,63,63,63,64,64,64,64, |
| 850 |
64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64, |
| 851 |
64,64,64,64,64,64,65,65,65,65,65,65,65,65,65,65, |
| 852 |
65,65,65,65,65,65,65,65,65,65,65,65,65,65,65,65, |
| 853 |
65,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66, |
| 854 |
66,66,66,66,66,66,66,66,66,66,66,67,67,67,67,67, |
| 855 |
67,67,67,67,67,67,67,67,67,67,67,67,67,67,67,67, |
| 856 |
67,67,67,67,67,67,68,68,68,68,68,68,68,68,68,68, |
| 857 |
68,68,68,68,68,68,68,68,68,68,68,68,68,68,68,68, |
| 858 |
68,69,69,69,69,69,69,69,69,69,69,69,69,69,69,69, |
| 859 |
69,69,69,69,69,69,69,69,69,69,69,70,70,70,70,70, |
| 860 |
70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70, |
| 861 |
70,70,70,70,70,70,71,71,71,71,71,71,71,71,71,71, |
| 862 |
71,71,71,71,71,71,71,71,71,71,71,71,71,71,71,71, |
| 863 |
72,72,72,72,72,72,72,72,72,72,72,72,72,72,72,72, |
| 864 |
72,72,72,72,72,72,72,72,72,72,72,73,73,73,73,73, |
| 865 |
73,73,73,73,73,73,73,73,73,73,73,73,73,73,73,73, |
| 866 |
73,73,73,73,73,74,74,74,74,74,74,74,74,74,74,74, |
| 867 |
74,74,74,74,74,74,74,74,74,74,74,74,74,74,74,74, |
| 868 |
75,75,75,75,75,75,75,75,75,75,75,75,75,75,75,75, |
| 869 |
75,75,75,75,75,75,75,75,75,75,75,76,76,76,76,76, |
| 870 |
76,76,76,76,76,76,76,76,76,76,76,76,76,76,76,76, |
| 871 |
76,76,76,76,76,77,77,77,77,77,77,77,77,77,77,77, |
| 872 |
77,77,77,77,77,77,77,77,77,77,77,77,77,77,77,77, |
| 873 |
78,78,78,78,78,78,78,78,78,78,78,78,78,78,78,78, |
| 874 |
78,78,78,78,78,78,78,78,78,78,79,79,79,79,79,79, |
| 875 |
79,79,79,79,79,79,79,79,79,79,79,79,79,79,79,79, |
| 876 |
79,79,79,79,79,80,80,80,80,80,80,80,80,80,80,80, |
| 877 |
80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80, |
| 878 |
81,81,81,81,81,81,81,81,81,81,81,81,81,81,81,81, |
| 879 |
81,81,81,81,81,81,81,81,81,81,82,82,82,82,82,82, |
| 880 |
82,82,82,82,82,82,82,82,82,82,82,82,82,82,82,82, |
| 881 |
82,82,82,82,82,83,83,83,83,83,83,83,83,83,83,83, |
| 882 |
83,83,83,83,83,83,83,83,83,83,83,83,83,83,83,84, |
| 883 |
84,84,84,84,84,84,84,84,84,84,84,84,84,84,84,84, |
| 884 |
84,84,84,84,84,84,84,84,84,84,85,85,85,85,85,85, |
| 885 |
85,85,85,85,85,85,85,85,85,85,85,85,85,85,85,85, |
| 886 |
85,85,85,85,86,86,86,86,86,86,86,86,86,86,86,86, |
| 887 |
86,86,86,86,86,86,86,86,86,86,86,86,86,86,86,86, |
| 888 |
86,86,86,86,86,86,86,86,86,86,86,86,86,86,86,86, |
| 889 |
86,86,86,86,86,86,86,86,86); |
| 890 |
|
| 891 |
DoubleToStringPowerOfTenDecimalExponentTable:array[-348..(340+8)-1] of TPasDblStrUtilsUInt8=(0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2, |
| 892 |
2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4, |
| 893 |
4,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6, |
| 894 |
6,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8, |
| 895 |
8,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10, |
| 896 |
10,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12, |
| 897 |
12,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14, |
| 898 |
14,15,15,15,15,15,15,15,15,16,16,16,16,16,16,16, |
| 899 |
16,17,17,17,17,17,17,17,17,18,18,18,18,18,18,18, |
| 900 |
18,19,19,19,19,19,19,19,19,20,20,20,20,20,20,20, |
| 901 |
20,21,21,21,21,21,21,21,21,22,22,22,22,22,22,22, |
| 902 |
22,23,23,23,23,23,23,23,23,24,24,24,24,24,24,24, |
| 903 |
24,25,25,25,25,25,25,25,25,26,26,26,26,26,26,26, |
| 904 |
26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28, |
| 905 |
28,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30, |
| 906 |
30,31,31,31,31,31,31,31,31,32,32,32,32,32,32,32, |
| 907 |
32,33,33,33,33,33,33,33,33,34,34,34,34,34,34,34, |
| 908 |
34,35,35,35,35,35,35,35,35,36,36,36,36,36,36,36, |
| 909 |
36,37,37,37,37,37,37,37,37,38,38,38,38,38,38,38, |
| 910 |
38,39,39,39,39,39,39,39,39,40,40,40,40,40,40,40, |
| 911 |
40,41,41,41,41,41,41,41,41,42,42,42,42,42,42,42, |
| 912 |
42,43,43,43,43,43,43,43,43,44,44,44,44,44,44,44, |
| 913 |
44,45,45,45,45,45,45,45,45,46,46,46,46,46,46,46, |
| 914 |
46,47,47,47,47,47,47,47,47,48,48,48,48,48,48,48, |
| 915 |
48,49,49,49,49,49,49,49,49,50,50,50,50,50,50,50, |
| 916 |
50,51,51,51,51,51,51,51,51,52,52,52,52,52,52,52, |
| 917 |
52,53,53,53,53,53,53,53,53,54,54,54,54,54,54,54, |
| 918 |
54,55,55,55,55,55,55,55,55,56,56,56,56,56,56,56, |
| 919 |
56,57,57,57,57,57,57,57,57,58,58,58,58,58,58,58, |
| 920 |
58,59,59,59,59,59,59,59,59,60,60,60,60,60,60,60, |
| 921 |
60,61,61,61,61,61,61,61,61,62,62,62,62,62,62,62, |
| 922 |
62,63,63,63,63,63,63,63,63,64,64,64,64,64,64,64, |
| 923 |
64,65,65,65,65,65,65,65,65,66,66,66,66,66,66,66, |
| 924 |
66,67,67,67,67,67,67,67,67,68,68,68,68,68,68,68, |
| 925 |
68,69,69,69,69,69,69,69,69,70,70,70,70,70,70,70, |
| 926 |
70,71,71,71,71,71,71,71,71,72,72,72,72,72,72,72, |
| 927 |
72,73,73,73,73,73,73,73,73,74,74,74,74,74,74,74, |
| 928 |
74,75,75,75,75,75,75,75,75,76,76,76,76,76,76,76, |
| 929 |
76,77,77,77,77,77,77,77,77,78,78,78,78,78,78,78, |
| 930 |
78,79,79,79,79,79,79,79,79,80,80,80,80,80,80,80, |
| 931 |
80,81,81,81,81,81,81,81,81,82,82,82,82,82,82,82, |
| 932 |
82,83,83,83,83,83,83,83,83,84,84,84,84,84,84,84, |
| 933 |
84,85,85,85,85,85,85,85,85,86,86,86,86,86,86,86, |
| 934 |
86,86,86,86,86,86,86,86); |
| 935 |
|
| 936 |
DoubleToStringEstimatePowerFactorTable:array[2..36] of TPasDblStrUtilsInt64=(4294967296, // round((ln(2)/ln(Radix))*4294967296.0); |
| 937 |
2709822658, |
| 938 |
2147483648, |
| 939 |
1849741732, |
| 940 |
1661520155, |
| 941 |
1529898219, |
| 942 |
1431655765, |
| 943 |
1354911329, |
| 944 |
1292913986, |
| 945 |
1241523975, |
| 946 |
1198050829, |
| 947 |
1160664035, |
| 948 |
1128071163, |
| 949 |
1099331346, |
| 950 |
1073741824, |
| 951 |
1050766077, |
| 952 |
1029986701, |
| 953 |
1011073584, |
| 954 |
993761859, |
| 955 |
977836272, |
| 956 |
963119891, |
| 957 |
949465783, |
| 958 |
936750801, |
| 959 |
924870866, |
| 960 |
913737342, |
| 961 |
903274219, |
| 962 |
893415894, |
| 963 |
884105413, |
| 964 |
875293062, |
| 965 |
866935226, |
| 966 |
858993459, |
| 967 |
851433729, |
| 968 |
844225782, |
| 969 |
837342623, |
| 970 |
830760078); |
| 971 |
|
| 972 |
function ConvertStringToDouble(const StringValue:TPasDblStrUtilsString;const RoundingMode:TPasDblStrUtilsRoundingMode=rmNearest;const OK:PPasDblStrUtilsBoolean=nil;const Base:TPasDblStrUtilsInt32=-1):TPasDblStrUtilsDouble; |
| 973 |
type PDoubleCasted=^TDoubleCasted; |
| 974 |
TDoubleCasted=packed record |
| 975 |
case TPasDblStrUtilsUInt8 of |
| 976 |
0:(Value:TPasDblStrUtilsDouble); |
| 977 |
1:({$ifdef BIG_ENDIAN}Hi,Lo{$else}Lo,Hi{$endif}:TPasDblStrUtilsUInt32); |
| 978 |
2:(Value64:TPasDblStrUtilsInt64); |
| 979 |
end; |
| 980 |
const MantissaWords=12; //6; // 12 |
| 981 |
MantissaDigits=52; //28; // 52 |
| 982 |
WordTopBit=$8000; |
| 983 |
WordBits=16; |
| 984 |
WordBitShift=4; |
| 985 |
WordBitMask=WordBits-1; |
| 986 |
WordMask=$ffff; |
| 987 |
IEEEFormatBytes=8; |
| 988 |
IEEEFormatBits=IEEEFormatBytes shl 3; |
| 989 |
IEEEFormatExplicit=0; |
| 990 |
IEEEFormatExponent=11; |
| 991 |
IEEEFormatOneMask=WordTopBit shr ((IEEEFormatExponent+IEEEFormatExplicit) and WordBitMask); |
| 992 |
IEEEFormatOnePos=(IEEEFormatExponent+IEEEFormatExplicit) shr WordBitShift; |
| 993 |
IEEEFormatExpMax=1 shl (IEEEFormatExponent-1); |
| 994 |
Bit53=TPasDblStrUtilsInt64(TPasDblStrUtilsInt64(1) shl 53); |
| 995 |
InvBit53Mask=TPasDblStrUtilsInt64($ffe0000000000000); |
| 996 |
MaximumMultiplier=TPasDblStrUtilsUInt32(TPasDblStrUtilsUInt32($ffffffff) div 36); |
| 997 |
DtoAFPUExceptionMask:TFPUExceptionMask=[exInvalidOp,exDenormalized,exZeroDivide,exOverflow,exUnderflow,exPrecision]; |
| 998 |
DtoAFPUPrecisionMode:TFPUPrecisionMode=pmDOUBLE; |
| 999 |
MaxFastPathDigits=16; |
| 1000 |
TenPowers:array[0..18] of TPasDblStrUtilsInt64= |
| 1001 |
( |
| 1002 |
1, |
| 1003 |
10, |
| 1004 |
100, |
| 1005 |
1000, |
| 1006 |
10000, |
| 1007 |
100000, |
| 1008 |
1000000, |
| 1009 |
10000000, |
| 1010 |
100000000, |
| 1011 |
1000000000, |
| 1012 |
10000000000, |
| 1013 |
100000000000, |
| 1014 |
1000000000000, |
| 1015 |
10000000000000, |
| 1016 |
100000000000000, |
| 1017 |
1000000000000000, |
| 1018 |
10000000000000000, |
| 1019 |
100000000000000000, |
| 1020 |
1000000000000000000 |
| 1021 |
); |
| 1022 |
type PWords=^TWords; |
| 1023 |
TWords=array[0..MantissaWords] of TPasDblStrUtilsUInt16; |
| 1024 |
PTemp=^TTemp; |
| 1025 |
TTemp=array[0..MantissaWords*2] of TPasDblStrUtilsUInt32; |
| 1026 |
PDigits=^TDigits; |
| 1027 |
TDigits=array[0..MantissaDigits] of TPasDblStrUtilsUInt8; |
| 1028 |
var MantissaPosition,Exponent,TenPower,TwoPower,ExtraTwos,Shift,i,DigitPos,StoredDigitPos,DigitPosBackwards,Digit,Overflow,OverflowBits,DroppedBits,DroppedBitsMask,MiddleValue,ExponentPower,ExponentValue:TPasDblStrUtilsInt32; |
| 1029 |
Bit,Carry:TPasDblStrUtilsUInt16; |
| 1030 |
Negative,ExponentNegative,HasDigits,Started,ZeroTail,Done:TPasDblStrUtilsBoolean; |
| 1031 |
ResultCasted:PDoubleCasted; |
| 1032 |
Temp:PTemp; |
| 1033 |
Digits:PDigits; |
| 1034 |
MantissaMultiplicator,Mantissa:PWords; |
| 1035 |
Value:TPasDblStrUtilsInt64; |
| 1036 |
c:TPasDblStrUtilsChar; |
| 1037 |
Part,Multiplier,NextMultiplier:TPasDblStrUtilsUInt32; |
| 1038 |
OldFPUExceptionMask:TFPUExceptionMask; |
| 1039 |
OldFPURoundingMode,NewFPURoundingMode:TFPURoundingMode; |
| 1040 |
OldFPUPrecisionMode:TFPUPrecisionMode; |
| 1041 |
function MantissaMultiply(vTo,vFrom:PWords):TPasDblStrUtilsInt32; |
| 1042 |
var i,j,k:TPasDblStrUtilsInt32; |
| 1043 |
v:TPasDblStrUtilsUInt32; |
| 1044 |
t:PTemp; |
| 1045 |
begin |
| 1046 |
t:=Temp; |
| 1047 |
FillChar(t^,sizeof(TTemp),#0); |
| 1048 |
for i:=0 to MantissaWords-1 do begin |
| 1049 |
for j:=0 to MantissaWords-1 do begin |
| 1050 |
v:=TPasDblStrUtilsUInt32(vTo^[i]+0)*TPasDblStrUtilsUInt32(vFrom^[j]+0); |
| 1051 |
k:=i+j; |
| 1052 |
inc(t^[k],v shr WordBits); |
| 1053 |
inc(t^[k+1],v and WordMask); |
| 1054 |
end; |
| 1055 |
end; |
| 1056 |
for i:=high(TTemp) downto 1 do begin |
| 1057 |
inc(t^[i-1],t^[i] shr WordBits); |
| 1058 |
t^[i]:=t^[i] and WordMask; |
| 1059 |
end; |
| 1060 |
if (t^[0] and WordTopBit)<>0 then begin |
| 1061 |
for i:=0 to MantissaWords-1 do begin |
| 1062 |
vTo^[i]:=t^[i] and WordMask; |
| 1063 |
end; |
| 1064 |
result:=0; |
| 1065 |
end else begin |
| 1066 |
for i:=0 to MantissaWords-1 do begin |
| 1067 |
vTo^[i]:=(t^[i] shl 1)+TPasDblStrUtilsUInt16(ord((t^[i+1] and WordTopBit)<>0)); |
| 1068 |
end; |
| 1069 |
result:=-1; |
| 1070 |
end; |
| 1071 |
end; |
| 1072 |
procedure MantissaShiftRight(var Mantissa:TWords;Shift:TPasDblStrUtilsInt32); |
| 1073 |
var Bits,Words,InvBits,Position:TPasDblStrUtilsInt32; |
| 1074 |
Carry,Current:TPasDblStrUtilsUInt32; |
| 1075 |
begin |
| 1076 |
Bits:=Shift and WordBitMask; |
| 1077 |
Words:=Shift shr WordBitShift; |
| 1078 |
InvBits:=WordBits-Bits; |
| 1079 |
Position:=high(TWords); |
| 1080 |
if Bits=0 then begin |
| 1081 |
if Words<>0 then begin |
| 1082 |
while Position>=Words do begin |
| 1083 |
Mantissa[Position]:=Mantissa[Position-Words]; |
| 1084 |
dec(Position); |
| 1085 |
end; |
| 1086 |
end; |
| 1087 |
end else begin |
| 1088 |
if (high(TWords)-Words)>=0 then begin |
| 1089 |
Carry:=Mantissa[high(TWords)-Words] shr Bits; |
| 1090 |
end else begin |
| 1091 |
Carry:=0; |
| 1092 |
end; |
| 1093 |
while Position>Words do begin |
| 1094 |
Current:=Mantissa[Position-(Words+1)]; |
| 1095 |
Mantissa[Position]:=(Current shl InvBits) or Carry; |
| 1096 |
Carry:=Current shr Bits; |
| 1097 |
dec(Position); |
| 1098 |
end; |
| 1099 |
Mantissa[Position]:=Carry; |
| 1100 |
dec(Position); |
| 1101 |
end; |
| 1102 |
while Position>=0 do begin |
| 1103 |
Mantissa[Position]:=0; |
| 1104 |
dec(Position); |
| 1105 |
end; |
| 1106 |
end; |
| 1107 |
procedure MantissaSetBit(var Mantissa:TWords;i:TPasDblStrUtilsInt32); {$ifdef CanInline}inline;{$endif} |
| 1108 |
begin |
| 1109 |
Mantissa[i shr WordBitShift]:=Mantissa[i shr WordBitShift] or (WordTopBit shr (i and WordBitMask)); |
| 1110 |
end; |
| 1111 |
function MantissaTestBit(var Mantissa:TWords;i:TPasDblStrUtilsInt32):TPasDblStrUtilsBoolean; {$ifdef CanInline}inline;{$endif} |
| 1112 |
begin |
| 1113 |
result:=(Mantissa[i shr WordBitShift] shr ((not i) and WordBitMask))<>0; |
| 1114 |
end; |
| 1115 |
function MantissaIsZero(var Mantissa:TWords):TPasDblStrUtilsBoolean; |
| 1116 |
var i:TPasDblStrUtilsInt32; |
| 1117 |
begin |
| 1118 |
result:=true; |
| 1119 |
for i:=low(TWords) to High(TWords) do begin |
| 1120 |
if Mantissa[i]<>0 then begin |
| 1121 |
result:=false; |
| 1122 |
break; |
| 1123 |
end; |
| 1124 |
end; |
| 1125 |
end; |
| 1126 |
function MantissaRound(Negative:TPasDblStrUtilsBoolean;var Mantissa:TWords;BitPos:TPasDblStrUtilsInt32):TPasDblStrUtilsBoolean; |
| 1127 |
var i,p:TPasDblStrUtilsInt32; |
| 1128 |
Bit:TPasDblStrUtilsUInt32; |
| 1129 |
function RoundAbsDown:TPasDblStrUtilsBoolean; |
| 1130 |
var j:TPasDblStrUtilsInt32; |
| 1131 |
begin |
| 1132 |
Mantissa[i]:=Mantissa[i] and not (Bit-1); |
| 1133 |
for j:=i+1 to high(TWords) do begin |
| 1134 |
Mantissa[j]:=0; |
| 1135 |
end; |
| 1136 |
result:=false; |
| 1137 |
end; |
| 1138 |
function RoundAbsUp:TPasDblStrUtilsBoolean; |
| 1139 |
var j:TPasDblStrUtilsInt32; |
| 1140 |
begin |
| 1141 |
Mantissa[i]:=(Mantissa[i] and not (Bit-1))+Bit; |
| 1142 |
for j:=i+1 to high(TWords) do begin |
| 1143 |
Mantissa[j]:=0; |
| 1144 |
end; |
| 1145 |
while (i>0) and (Mantissa[i]=0) do begin |
| 1146 |
dec(i); |
| 1147 |
inc(Mantissa[i]); |
| 1148 |
end; |
| 1149 |
result:=Mantissa[0]=0; |
| 1150 |
end; |
| 1151 |
function RoundTowardsInfinity:TPasDblStrUtilsBoolean; |
| 1152 |
var j:TPasDblStrUtilsInt32; |
| 1153 |
m:TPasDblStrUtilsUInt32; |
| 1154 |
begin |
| 1155 |
m:=Mantissa[i] and ((Bit shl 1)-1); |
| 1156 |
for j:=i+1 to high(TWords) do begin |
| 1157 |
m:=m or Mantissa[j]; |
| 1158 |
end; |
| 1159 |
if m<>0 then begin |
| 1160 |
result:=RoundAbsUp; |
| 1161 |
end else begin |
| 1162 |
result:=RoundAbsDown; |
| 1163 |
end; |
| 1164 |
end; |
| 1165 |
function RoundNear:TPasDblStrUtilsBoolean; |
| 1166 |
var j:TPasDblStrUtilsInt32; |
| 1167 |
m:TPasDblStrUtilsUInt32; |
| 1168 |
begin |
| 1169 |
if (Mantissa[i] and Bit)<>0 then begin |
| 1170 |
Mantissa[i]:=Mantissa[i] and not Bit; |
| 1171 |
m:=Mantissa[i] and ((Bit shl 1)-1); |
| 1172 |
for j:=i+1 to high(TWords) do begin |
| 1173 |
m:=m or Mantissa[j]; |
| 1174 |
end; |
| 1175 |
Mantissa[i]:=Mantissa[i] or Bit; |
| 1176 |
if m<>0 then begin |
| 1177 |
result:=RoundAbsUp; |
| 1178 |
end else begin |
| 1179 |
if MantissaTestBit(Mantissa,BitPos-1) then begin |
| 1180 |
result:=RoundAbsUp; |
| 1181 |
end else begin |
| 1182 |
result:=RoundAbsDown; |
| 1183 |
end; |
| 1184 |
end; |
| 1185 |
end else begin |
| 1186 |
result:=RoundAbsDown; |
| 1187 |
end; |
| 1188 |
end; |
| 1189 |
begin |
| 1190 |
i:=BitPos shr WordBitShift; |
| 1191 |
p:=BitPos and WordBitMask; |
| 1192 |
Bit:=WordTopBit shr p; |
| 1193 |
case RoundingMode of |
| 1194 |
rmNearest:begin |
| 1195 |
result:=RoundNear; |
| 1196 |
end; |
| 1197 |
rmTruncate:begin |
| 1198 |
result:=RoundAbsDown; |
| 1199 |
end; |
| 1200 |
rmUp:begin |
| 1201 |
if Negative then begin |
| 1202 |
result:=RoundAbsDown; |
| 1203 |
end else begin |
| 1204 |
result:=RoundTowardsInfinity; |
| 1205 |
end; |
| 1206 |
end; |
| 1207 |
rmDown:begin |
| 1208 |
if Negative then begin |
| 1209 |
result:=RoundTowardsInfinity; |
| 1210 |
end else begin |
| 1211 |
result:=RoundAbsDown; |
| 1212 |
end; |
| 1213 |
end; |
| 1214 |
else begin |
| 1215 |
result:=false; |
| 1216 |
end; |
| 1217 |
end; |
| 1218 |
end; |
| 1219 |
function CountLeadingZeros32(a:TPasDblStrUtilsUInt32):TPasDblStrUtilsInt32; |
| 1220 |
const CountLeadingZerosHigh:array[TPasDblStrUtilsUInt8] of TPasDblStrUtilsUInt8=(8,7,6,6,5,5,5,5,4,4,4,4,4,4,4,4, |
| 1221 |
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, |
| 1222 |
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, |
| 1223 |
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, |
| 1224 |
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 1225 |
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 1226 |
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 1227 |
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 1228 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1229 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1230 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1231 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1232 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1233 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1234 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 1235 |
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0); |
| 1236 |
begin |
| 1237 |
result:=0; |
| 1238 |
if a<$10000 then begin |
| 1239 |
inc(result,16); |
| 1240 |
a:=a shl 16; |
| 1241 |
end; |
| 1242 |
if a<$1000000 then begin |
| 1243 |
inc(result,8); |
| 1244 |
a:=a shl 8; |
| 1245 |
end; |
| 1246 |
inc(result,CountLeadingZerosHigh[a shr 24]); |
| 1247 |
end; |
| 1248 |
function CountLeadingZeros64(a:TPasDblStrUtilsInt64):TPasDblStrUtilsInt32; |
| 1249 |
begin |
| 1250 |
if a<TPasDblStrUtilsInt64($100000000) then begin |
| 1251 |
result:=32; |
| 1252 |
end else begin |
| 1253 |
result:=0; |
| 1254 |
a:=a shr 32; |
| 1255 |
end; |
| 1256 |
inc(result,CountLeadingZeros32(a)); |
| 1257 |
end; |
| 1258 |
begin |
| 1259 |
if assigned(OK) then begin |
| 1260 |
OK^:=false; |
| 1261 |
end; |
| 1262 |
ResultCasted:=pointer(@result); |
| 1263 |
ResultCasted^.Hi:=$7ff80000; |
| 1264 |
ResultCasted^.Lo:=$00000000; |
| 1265 |
i:=1; |
| 1266 |
while (i<=length(StringValue)) and (StringValue[i] in [#0..#32]) do begin |
| 1267 |
inc(i); |
| 1268 |
end; |
| 1269 |
if (i<=length(StringValue)) and ((StringValue[i]='-') or (StringValue[i]='+')) then begin |
| 1270 |
Negative:=StringValue[i]='-'; |
| 1271 |
inc(i); |
| 1272 |
end else begin |
| 1273 |
Negative:=false; |
| 1274 |
end; |
| 1275 |
HasDigits:=false; |
| 1276 |
if ((i+7)<=length(StringValue)) and ((StringValue[i]='I') and (StringValue[i+1]='n') and (StringValue[i+2]='f') and (StringValue[i+3]='i') and (StringValue[i+4]='n') and (StringValue[i+5]='i') and (StringValue[i+6]='t') and (StringValue[i+7]='y')) then begin |
| 1277 |
if Negative then begin |
| 1278 |
ResultCasted^.Hi:=$fff00000; |
| 1279 |
ResultCasted^.Lo:=$00000000; |
| 1280 |
end else begin |
| 1281 |
ResultCasted^.Hi:=$7ff00000; |
| 1282 |
ResultCasted^.Lo:=$00000000; |
| 1283 |
end; |
| 1284 |
if assigned(OK) then begin |
| 1285 |
OK^:=true; |
| 1286 |
end; |
| 1287 |
end else if ((i+2)<=length(StringValue)) and ((StringValue[i]='N') and (StringValue[i+1]='a') and (StringValue[i+2]='N')) then begin |
| 1288 |
ResultCasted^.Hi:=$7ff80000; |
| 1289 |
ResultCasted^.Lo:=$00000000; |
| 1290 |
if assigned(OK) then begin |
| 1291 |
OK^:=true; |
| 1292 |
end; |
| 1293 |
end else if (Base in [2,4,8,16,32]) or ((not (Base in [2..36])) and ((((i+1)<=length(StringValue)) and ((StringValue[i]='0') and (StringValue[i+1] in ['b','o','x']))))) then begin |
| 1294 |
ResultCasted^.Hi:=$00000000; |
| 1295 |
ResultCasted^.Lo:=$00000000; |
| 1296 |
case Base of |
| 1297 |
2:begin |
| 1298 |
Shift:=1; |
| 1299 |
end; |
| 1300 |
4:begin |
| 1301 |
Shift:=2; |
| 1302 |
end; |
| 1303 |
8:begin |
| 1304 |
Shift:=3; |
| 1305 |
end; |
| 1306 |
16:begin |
| 1307 |
Shift:=4; |
| 1308 |
end; |
| 1309 |
32:begin |
| 1310 |
Shift:=5; |
| 1311 |
end; |
| 1312 |
else begin |
| 1313 |
inc(i); |
| 1314 |
case StringValue[i] of |
| 1315 |
'b':begin |
| 1316 |
Shift:=1; |
| 1317 |
end; |
| 1318 |
'o':begin |
| 1319 |
Shift:=3; |
| 1320 |
end; |
| 1321 |
else {'x':}begin |
| 1322 |
Shift:=4; |
| 1323 |
end; |
| 1324 |
end; |
| 1325 |
inc(i); |
| 1326 |
end; |
| 1327 |
end; |
| 1328 |
TwoPower:=1 shl Shift; |
| 1329 |
Value:=0; |
| 1330 |
Exponent:=0; |
| 1331 |
while (i<=length(StringValue)) and (StringValue[i]='0') do begin |
| 1332 |
HasDigits:=true; |
| 1333 |
inc(i); |
| 1334 |
end; |
| 1335 |
if i<=length(StringValue) then begin |
| 1336 |
//q:=0; |
| 1337 |
ExponentPower:=1; |
| 1338 |
Digit:=0; |
| 1339 |
while i<=length(StringValue) do begin |
| 1340 |
c:=StringValue[i]; |
| 1341 |
if c='.' then begin |
| 1342 |
if ExponentPower>0 then begin |
| 1343 |
ExponentPower:=0; |
| 1344 |
inc(i); |
| 1345 |
if i>length(StringValue) then begin |
| 1346 |
Done:=true; |
| 1347 |
if Done then begin |
| 1348 |
end; |
| 1349 |
break; |
| 1350 |
end; |
| 1351 |
continue; |
| 1352 |
end else begin |
| 1353 |
break; |
| 1354 |
end; |
| 1355 |
end else if (c in ['0'..'9']) and (ord(c)<=(ord('0')+TwoPower)) then begin |
| 1356 |
Digit:=ord(c)-ord('0'); |
| 1357 |
end else if (TwoPower>10) and ((c in ['a'..'z']) and (ord(c)<=((ord('a')+TwoPower)-10))) then begin |
| 1358 |
Digit:=(ord(c)-ord('a'))+10; |
| 1359 |
end else if (TwoPower>10) and ((c in ['A'..'Z']) and (ord(c)<=((ord('A')+TwoPower)-10))) then begin |
| 1360 |
Digit:=(ord(c)-ord('A'))+10; |
| 1361 |
end else begin |
| 1362 |
break; |
| 1363 |
end; |
| 1364 |
inc(i); |
| 1365 |
HasDigits:=true; |
| 1366 |
if ExponentPower<=0 then begin |
| 1367 |
dec(ExponentPower); |
| 1368 |
end; |
| 1369 |
Value:=(Value shl Shift) or Digit; |
| 1370 |
Overflow:=TPasDblStrUtilsInt32(TPasDblStrUtilsInt64(Value shr 53)); |
| 1371 |
if Overflow<>0 then begin |
| 1372 |
OverflowBits:=1; |
| 1373 |
while Overflow>1 do begin |
| 1374 |
inc(OverflowBits); |
| 1375 |
Overflow:=Overflow shr 1; |
| 1376 |
end; |
| 1377 |
DroppedBitsMask:=(1 shl OverflowBits)-1; |
| 1378 |
DroppedBits:=Value and DroppedBitsMask; |
| 1379 |
Value:=Value shr OverflowBits; |
| 1380 |
Exponent:=OverflowBits; |
| 1381 |
ZeroTail:=true; |
| 1382 |
while i<=length(StringValue) do begin |
| 1383 |
c:=StringValue[i]; |
| 1384 |
if (c in ['0'..'9']) and (ord(c)<=(ord('0')+TwoPower)) then begin |
| 1385 |
Digit:=ord(c)-ord('0'); |
| 1386 |
end else if (TwoPower>10) and ((c in ['a'..'z']) and (ord(c)<=((ord('a')+TwoPower)-10))) then begin |
| 1387 |
Digit:=(ord(c)-ord('a'))+10; |
| 1388 |
end else if (TwoPower>10) and ((c in ['A'..'Z']) and (ord(c)<=((ord('A')+TwoPower)-10))) then begin |
| 1389 |
Digit:=(ord(c)-ord('A'))+10; |
| 1390 |
end else begin |
| 1391 |
break; |
| 1392 |
end; |
| 1393 |
inc(i); |
| 1394 |
if Digit<>0 then begin |
| 1395 |
ZeroTail:=false; |
| 1396 |
end; |
| 1397 |
inc(Exponent,Shift); |
| 1398 |
end; |
| 1399 |
MiddleValue:=1 shl (OverflowBits-1); |
| 1400 |
if DroppedBits>MiddleValue then begin |
| 1401 |
inc(Value); |
| 1402 |
end else if DroppedBits=MiddleValue then begin |
| 1403 |
if ((Value and 1)<>0) or not ZeroTail then begin |
| 1404 |
inc(Value); |
| 1405 |
end; |
| 1406 |
end; |
| 1407 |
while (Value and Bit53)<>0 do begin |
| 1408 |
Value:=Value shr 1; |
| 1409 |
inc(Exponent); |
| 1410 |
end; |
| 1411 |
break; |
| 1412 |
end; |
| 1413 |
end; |
| 1414 |
if ExponentPower>0 then begin |
| 1415 |
ExponentPower:=0; |
| 1416 |
end; |
| 1417 |
ExponentValue:=0; |
| 1418 |
ExponentNegative:=false; |
| 1419 |
if (i<=length(StringValue)) and (StringValue[i] in ['e','E','p','P']) then begin |
| 1420 |
inc(i); |
| 1421 |
if (i<=length(StringValue)) and (StringValue[i] in ['+','-']) then begin |
| 1422 |
ExponentNegative:=StringValue[i]='-'; |
| 1423 |
inc(i); |
| 1424 |
end; |
| 1425 |
HasDigits:=false; |
| 1426 |
while (i<=length(StringValue)) and (StringValue[i] in ['0'..'9']) do begin |
| 1427 |
ExponentValue:=(ExponentValue*10)+TPasDblStrUtilsInt32(ord(StringValue[i])-ord('0')); |
| 1428 |
HasDigits:=true; |
| 1429 |
inc(i); |
| 1430 |
end; |
| 1431 |
end; |
| 1432 |
if ExponentNegative then begin |
| 1433 |
dec(ExponentPower,ExponentValue); |
| 1434 |
end else begin |
| 1435 |
inc(ExponentPower,ExponentValue); |
| 1436 |
end; |
| 1437 |
inc(Exponent,Shift*ExponentPower); |
| 1438 |
Shift:=CountLeadingZeros64(Value); |
| 1439 |
ExponentValue:=$432-((Shift-IEEEFormatExponent)-Exponent); |
| 1440 |
if (((ExponentValue>$34) and (ExponentValue<$7fe)) and (Exponent<IEEEFormatExponent)) and (Value<Bit53) then begin |
| 1441 |
dec(Shift,IEEEFormatExponent); |
| 1442 |
if Shift>=0 then begin |
| 1443 |
Value:=Value shl Shift; |
| 1444 |
end else begin |
| 1445 |
Value:=Value shr (-Shift); |
| 1446 |
end; |
| 1447 |
ResultCasted^.Value64:=((TPasDblStrUtilsInt64(ExponentValue) shl 52)+Value) and TPasDblStrUtilsInt64($7fffffffffffffff); |
| 1448 |
end else begin |
| 1449 |
New(Mantissa); |
| 1450 |
try |
| 1451 |
FillChar(Mantissa^,sizeof(TWords),#0); |
| 1452 |
Value:=Value shl Shift; |
| 1453 |
inc(Exponent,64-Shift); |
| 1454 |
Mantissa^[0]:=(Value shr 48) and $ffff; |
| 1455 |
Mantissa^[1]:=(Value shr 32) and $ffff; |
| 1456 |
Mantissa^[2]:=(Value shr 16) and $ffff; |
| 1457 |
Mantissa^[3]:=(Value shr 0) and $ffff; |
| 1458 |
if (Mantissa^[0] and WordTopBit)<>0 then begin |
| 1459 |
dec(Exponent); |
| 1460 |
if (Exponent>=(2-IEEEFormatExpMax)) and (Exponent<=IEEEFormatExpMax) then begin |
| 1461 |
inc(Exponent,IEEEFormatExpMax-1); |
| 1462 |
MantissaShiftRight(Mantissa^,IEEEFormatExponent+IEEEFormatExplicit); |
| 1463 |
MantissaRound(Negative,Mantissa^,IEEEFormatBits); |
| 1464 |
if MantissaTestBit(Mantissa^,IEEEFormatExponent+IEEEFormatExplicit-1) then begin |
| 1465 |
MantissaShiftRight(Mantissa^,1); |
| 1466 |
inc(Exponent); |
| 1467 |
end; |
| 1468 |
if Exponent>=(IEEEFormatExpMax shl 1)-1 then begin |
| 1469 |
ResultCasted^.Hi:=$7ff00000; |
| 1470 |
ResultCasted^.Lo:=$00000000; |
| 1471 |
end else begin |
| 1472 |
ResultCasted^.Hi:=(((Exponent shl 4) or (Mantissa^[0] and $f)) shl 16) or Mantissa^[1]; |
| 1473 |
ResultCasted^.Lo:=(Mantissa^[2] shl 16) or Mantissa^[3]; |
| 1474 |
end; |
| 1475 |
end else if Exponent>0 then begin |
| 1476 |
ResultCasted^.Hi:=$7ff00000; |
| 1477 |
ResultCasted^.Lo:=$00000000; |
| 1478 |
end else begin |
| 1479 |
Shift:=IEEEFormatExplicit-(Exponent+(IEEEFormatExpMax-(2+IEEEFormatExponent))); |
| 1480 |
MantissaShiftRight(Mantissa^,Shift); |
| 1481 |
MantissaRound(Negative,Mantissa^,IEEEFormatBits); |
| 1482 |
if (Mantissa^[IEEEFormatOnePos] and IEEEFormatOneMask)<>0 then begin |
| 1483 |
Exponent:=1; |
| 1484 |
if IEEEFormatExplicit=0 then begin |
| 1485 |
Mantissa^[IEEEFormatOnePos]:=Mantissa^[IEEEFormatOnePos] and not IEEEFormatOneMask; |
| 1486 |
end; |
| 1487 |
Mantissa^[0]:=Mantissa^[0] or (Exponent shl (WordBitMask-IEEEFormatExponent)); |
| 1488 |
ResultCasted^.Hi:=(((Exponent shl 4) or (Mantissa^[0] and $f)) shl 16) or Mantissa^[1]; |
| 1489 |
ResultCasted^.Lo:=(Mantissa^[2] shl 16) or Mantissa^[3]; |
| 1490 |
end else begin |
| 1491 |
if MantissaIsZero(Mantissa^) then begin |
| 1492 |
ResultCasted^.Hi:=$00000000; |
| 1493 |
ResultCasted^.Lo:=$00000000; |
| 1494 |
end else begin |
| 1495 |
ResultCasted^.Hi:=(Mantissa^[0] shl 16) or Mantissa^[1]; |
| 1496 |
ResultCasted^.Lo:=(Mantissa^[2] shl 16) or Mantissa^[3]; |
| 1497 |
end; |
| 1498 |
end; |
| 1499 |
end; |
| 1500 |
end else begin |
| 1501 |
ResultCasted^.Hi:=$00000000; |
| 1502 |
ResultCasted^.Lo:=$00000000; |
| 1503 |
end; |
| 1504 |
finally |
| 1505 |
Dispose(Mantissa); |
| 1506 |
end; |
| 1507 |
end; |
| 1508 |
end else begin |
| 1509 |
ResultCasted^.Hi:=$00000000; |
| 1510 |
ResultCasted^.Lo:=$00000000; |
| 1511 |
end; |
| 1512 |
if Negative then begin |
| 1513 |
ResultCasted^.Hi:=ResultCasted^.Hi or $80000000; |
| 1514 |
end; |
| 1515 |
if HasDigits then begin |
| 1516 |
if assigned(OK) then begin |
| 1517 |
OK^:=true; |
| 1518 |
end; |
| 1519 |
end; |
| 1520 |
end else if Base in [2..9,11..36] then begin |
| 1521 |
ResultCasted^.Hi:=$00000000; |
| 1522 |
ResultCasted^.Lo:=$00000000; |
| 1523 |
while (i<=length(StringValue)) and (StringValue[i]='0') do begin |
| 1524 |
HasDigits:=true; |
| 1525 |
inc(i); |
| 1526 |
end; |
| 1527 |
if i<=length(StringValue) then begin |
| 1528 |
case RoundingMode of |
| 1529 |
rmNearest:begin |
| 1530 |
NewFPURoundingMode:=rmNearest; |
| 1531 |
end; |
| 1532 |
rmTruncate:begin |
| 1533 |
NewFPURoundingMode:=rmTruncate; |
| 1534 |
end; |
| 1535 |
rmUp:begin |
| 1536 |
NewFPURoundingMode:=rmUp; |
| 1537 |
end; |
| 1538 |
rmDown:begin |
| 1539 |
NewFPURoundingMode:=rmDown; |
| 1540 |
end; |
| 1541 |
else begin |
| 1542 |
NewFPURoundingMode:=rmNearest; |
| 1543 |
end; |
| 1544 |
end; |
| 1545 |
OldFPUExceptionMask:=GetExceptionMask; |
| 1546 |
OldFPUPrecisionMode:=GetPrecisionMode; |
| 1547 |
OldFPURoundingMode:=GetRoundMode; |
| 1548 |
try |
| 1549 |
if OldFPUExceptionMask<>DtoAFPUExceptionMask then begin |
| 1550 |
SetExceptionMask(DtoAFPUExceptionMask); |
| 1551 |
end; |
| 1552 |
if OldFPUPrecisionMode<>DtoAFPUPrecisionMode then begin |
| 1553 |
SetPrecisionMode(DtoAFPUPrecisionMode); |
| 1554 |
end; |
| 1555 |
if OldFPURoundingMode<>NewFPURoundingMode then begin |
| 1556 |
SetRoundMode(NewFPURoundingMode); |
| 1557 |
end; |
| 1558 |
Part:=0; |
| 1559 |
Multiplier:=1; |
| 1560 |
Digit:=0; |
| 1561 |
Done:=false; |
| 1562 |
ExponentPower:=1; |
| 1563 |
while not Done do begin |
| 1564 |
while true do begin |
| 1565 |
c:=StringValue[i]; |
| 1566 |
if c='.' then begin |
| 1567 |
if ExponentPower>0 then begin |
| 1568 |
ExponentPower:=0; |
| 1569 |
inc(i); |
| 1570 |
if i>length(StringValue) then begin |
| 1571 |
Done:=true; |
| 1572 |
break; |
| 1573 |
end; |
| 1574 |
continue; |
| 1575 |
end else begin |
| 1576 |
Done:=true; |
| 1577 |
break; |
| 1578 |
end; |
| 1579 |
end else if ((c>='0') and (c<='9')) and (ord(c)<=(ord('0')+Base)) then begin |
| 1580 |
Digit:=ord(c)-ord('0'); |
| 1581 |
end else if (Base>10) and (((c>='a') and (c<='z')) and (ord(c)<=((ord('a')+Base)-10))) then begin |
| 1582 |
Digit:=(ord(c)-ord('a'))+10; |
| 1583 |
end else if (Base>10) and (((c>='A') and (c<='Z')) and (ord(c)<=((ord('A')+Base)-10))) then begin |
| 1584 |
Digit:=(ord(c)-ord('A'))+10; |
| 1585 |
end else begin |
| 1586 |
Done:=true; |
| 1587 |
break; |
| 1588 |
end; |
| 1589 |
HasDigits:=true; |
| 1590 |
NextMultiplier:=Multiplier*TPasDblStrUtilsUInt32(Base); |
| 1591 |
if NextMultiplier>MaximumMultiplier then begin |
| 1592 |
break; |
| 1593 |
end; |
| 1594 |
if ExponentPower<=0 then begin |
| 1595 |
dec(ExponentPower); |
| 1596 |
end; |
| 1597 |
Part:=(Part*TPasDblStrUtilsUInt32(Base))+TPasDblStrUtilsUInt32(Digit); |
| 1598 |
Multiplier:=NextMultiplier; |
| 1599 |
Assert(Multiplier>Part); |
| 1600 |
inc(i); |
| 1601 |
if i>length(StringValue) then begin |
| 1602 |
Done:=true; |
| 1603 |
break; |
| 1604 |
end; |
| 1605 |
end; |
| 1606 |
ResultCasted^.Value:=(ResultCasted^.Value*Multiplier)+Part; |
| 1607 |
end; |
| 1608 |
if ExponentPower>0 then begin |
| 1609 |
ExponentPower:=0; |
| 1610 |
end; |
| 1611 |
ExponentValue:=0; |
| 1612 |
ExponentNegative:=false; |
| 1613 |
if (i<=length(StringValue)) and (StringValue[i] in ['e','E','p','P']) then begin |
| 1614 |
inc(i); |
| 1615 |
if (i<=length(StringValue)) and (StringValue[i] in ['+','-']) then begin |
| 1616 |
ExponentNegative:=StringValue[i]='-'; |
| 1617 |
inc(i); |
| 1618 |
end; |
| 1619 |
HasDigits:=false; |
| 1620 |
while (i<=length(StringValue)) and (StringValue[i] in ['0'..'9']) do begin |
| 1621 |
ExponentValue:=(ExponentValue*10)+TPasDblStrUtilsInt32(ord(StringValue[i])-ord('0')); |
| 1622 |
HasDigits:=true; |
| 1623 |
inc(i); |
| 1624 |
end; |
| 1625 |
end; |
| 1626 |
if ExponentNegative then begin |
| 1627 |
dec(ExponentPower,ExponentValue); |
| 1628 |
end else begin |
| 1629 |
inc(ExponentPower,ExponentValue); |
| 1630 |
end; |
| 1631 |
if ExponentPower<>0 then begin |
| 1632 |
ResultCasted^.Value:=ResultCasted^.Value*power(Base,ExponentPower); |
| 1633 |
end; |
| 1634 |
finally |
| 1635 |
if OldFPUExceptionMask<>DtoAFPUExceptionMask then begin |
| 1636 |
SetExceptionMask(OldFPUExceptionMask); |
| 1637 |
end; |
| 1638 |
if OldFPUPrecisionMode<>DtoAFPUPrecisionMode then begin |
| 1639 |
SetPrecisionMode(OldFPUPrecisionMode); |
| 1640 |
end; |
| 1641 |
if OldFPURoundingMode<>NewFPURoundingMode then begin |
| 1642 |
SetRoundMode(OldFPURoundingMode); |
| 1643 |
end; |
| 1644 |
end; |
| 1645 |
end else begin |
| 1646 |
ResultCasted^.Hi:=$00000000; |
| 1647 |
ResultCasted^.Lo:=$00000000; |
| 1648 |
end; |
| 1649 |
if Negative then begin |
| 1650 |
ResultCasted^.Hi:=ResultCasted^.Hi or $80000000; |
| 1651 |
end; |
| 1652 |
if HasDigits then begin |
| 1653 |
if assigned(OK) then begin |
| 1654 |
OK^:=true; |
| 1655 |
end; |
| 1656 |
end; |
| 1657 |
end else begin |
| 1658 |
HasDigits:=false; |
| 1659 |
Value:=0; |
| 1660 |
StoredDigitPos:=i; |
| 1661 |
DigitPos:=0; |
| 1662 |
TenPower:=1; |
| 1663 |
while (i<=length(StringValue)) and (StringValue[i]='0') do begin |
| 1664 |
HasDigits:=true; |
| 1665 |
inc(i); |
| 1666 |
end; |
| 1667 |
while i<=length(StringValue) do begin |
| 1668 |
c:=StringValue[i]; |
| 1669 |
case c of |
| 1670 |
'0'..'9':begin |
| 1671 |
HasDigits:=true; |
| 1672 |
Value:=(Value*10)+(ord(c)-ord('0')); |
| 1673 |
inc(DigitPos); |
| 1674 |
if (Value and InvBit53Mask)<>0 then begin |
| 1675 |
HasDigits:=false; |
| 1676 |
break; |
| 1677 |
end; |
| 1678 |
if TenPower<=0 then begin |
| 1679 |
dec(TenPower); |
| 1680 |
if TenPower>high(TenPowers) then begin |
| 1681 |
HasDigits:=false; |
| 1682 |
break; |
| 1683 |
end; |
| 1684 |
end; |
| 1685 |
end; |
| 1686 |
'.':begin |
| 1687 |
if TenPower<=0 then begin |
| 1688 |
HasDigits:=false; |
| 1689 |
break; |
| 1690 |
end else begin |
| 1691 |
TenPower:=0; |
| 1692 |
end; |
| 1693 |
end; |
| 1694 |
'e','E':begin |
| 1695 |
break; |
| 1696 |
end; |
| 1697 |
else begin |
| 1698 |
HasDigits:=false; |
| 1699 |
break; |
| 1700 |
end; |
| 1701 |
end; |
| 1702 |
inc(i); |
| 1703 |
end; |
| 1704 |
if HasDigits then begin |
| 1705 |
if TenPower>0 then begin |
| 1706 |
TenPower:=0; |
| 1707 |
end; |
| 1708 |
ExponentValue:=0; |
| 1709 |
ExponentNegative:=false; |
| 1710 |
if (i<=length(StringValue)) and (StringValue[i] in ['e','E']) then begin |
| 1711 |
inc(i); |
| 1712 |
if (i<=length(StringValue)) and (StringValue[i] in ['+','-']) then begin |
| 1713 |
ExponentNegative:=StringValue[i]='-'; |
| 1714 |
inc(i); |
| 1715 |
end; |
| 1716 |
HasDigits:=false; |
| 1717 |
while (i<=length(StringValue)) and (StringValue[i] in ['0'..'9']) do begin |
| 1718 |
ExponentValue:=(ExponentValue*10)+TPasDblStrUtilsInt32(ord(StringValue[i])-ord('0')); |
| 1719 |
HasDigits:=true; |
| 1720 |
inc(i); |
| 1721 |
end; |
| 1722 |
end; |
| 1723 |
if Value=0 then begin |
| 1724 |
TenPower:=0; |
| 1725 |
end else begin |
| 1726 |
if ExponentNegative then begin |
| 1727 |
dec(TenPower,ExponentValue); |
| 1728 |
end else begin |
| 1729 |
inc(TenPower,ExponentValue); |
| 1730 |
end; |
| 1731 |
if TenPower<>0 then begin |
| 1732 |
if TenPower>0 then begin |
| 1733 |
if ((DigitPos+TenPower)>MaxFastPathDigits) or (TenPower>high(TenPowers)) then begin |
| 1734 |
HasDigits:=false; |
| 1735 |
end else begin |
| 1736 |
Value:=Value*TenPowers[TenPower]; |
| 1737 |
TenPower:=0; |
| 1738 |
if (Value and InvBit53Mask)<>0 then begin |
| 1739 |
HasDigits:=false; |
| 1740 |
end; |
| 1741 |
end; |
| 1742 |
end else begin |
| 1743 |
if (-TenPower)>high(TenPowers) then begin |
| 1744 |
HasDigits:=false; |
| 1745 |
end else begin |
| 1746 |
i:=-TenPower; |
| 1747 |
while (i>0) and (TenPower<0) do begin |
| 1748 |
if (Value mod TenPowers[i])=0 then begin |
| 1749 |
Value:=Value div TenPowers[i]; |
| 1750 |
inc(TenPower,i); |
| 1751 |
end else begin |
| 1752 |
if (i and 1)=0 then begin |
| 1753 |
i:=i shr 1; |
| 1754 |
end else begin |
| 1755 |
dec(i); |
| 1756 |
end; |
| 1757 |
end; |
| 1758 |
end; |
| 1759 |
end; |
| 1760 |
end; |
| 1761 |
end; |
| 1762 |
end; |
| 1763 |
end; |
| 1764 |
if HasDigits then begin |
| 1765 |
if Value=0 then begin |
| 1766 |
ResultCasted^.Hi:=$00000000; |
| 1767 |
ResultCasted^.Lo:=$00000000; |
| 1768 |
end else begin |
| 1769 |
Shift:=CountLeadingZeros64(Value)-11; |
| 1770 |
if Shift>=0 then begin |
| 1771 |
Value:=Value shl Shift; |
| 1772 |
end else begin |
| 1773 |
Value:=Value shr (-Shift); |
| 1774 |
end; |
| 1775 |
ResultCasted^.Value64:=((TPasDblStrUtilsInt64($432-Shift) shl 52)+Value) and TPasDblStrUtilsInt64($7fffffffffffffff); |
| 1776 |
end; |
| 1777 |
if TenPower<>0 then begin |
| 1778 |
case RoundingMode of |
| 1779 |
rmNearest:begin |
| 1780 |
NewFPURoundingMode:=rmNearest; |
| 1781 |
end; |
| 1782 |
rmTruncate:begin |
| 1783 |
NewFPURoundingMode:=rmTruncate; |
| 1784 |
end; |
| 1785 |
rmUp:begin |
| 1786 |
NewFPURoundingMode:=rmUp; |
| 1787 |
end; |
| 1788 |
rmDown:begin |
| 1789 |
NewFPURoundingMode:=rmDown; |
| 1790 |
end; |
| 1791 |
else begin |
| 1792 |
NewFPURoundingMode:=rmNearest; |
| 1793 |
end; |
| 1794 |
end; |
| 1795 |
OldFPUExceptionMask:=GetExceptionMask; |
| 1796 |
OldFPUPrecisionMode:=GetPrecisionMode; |
| 1797 |
OldFPURoundingMode:=GetRoundMode; |
| 1798 |
try |
| 1799 |
if OldFPUExceptionMask<>DtoAFPUExceptionMask then begin |
| 1800 |
SetExceptionMask(DtoAFPUExceptionMask); |
| 1801 |
end; |
| 1802 |
if OldFPUPrecisionMode<>DtoAFPUPrecisionMode then begin |
| 1803 |
SetPrecisionMode(DtoAFPUPrecisionMode); |
| 1804 |
end; |
| 1805 |
if OldFPURoundingMode<>NewFPURoundingMode then begin |
| 1806 |
SetRoundMode(NewFPURoundingMode); |
| 1807 |
end; |
| 1808 |
if TenPower>0 then begin |
| 1809 |
while TenPower>high(TenPowers) do begin |
| 1810 |
ResultCasted^.Value:=ResultCasted^.Value*TenPowers[high(TenPowers)]; |
| 1811 |
dec(TenPower,high(TenPowers)); |
| 1812 |
end; |
| 1813 |
if TenPower>0 then begin |
| 1814 |
ResultCasted^.Value:=ResultCasted^.Value*TenPowers[TenPower]; |
| 1815 |
end; |
| 1816 |
end else begin |
| 1817 |
TenPower:=-TenPower; |
| 1818 |
while TenPower>high(TenPowers) do begin |
| 1819 |
ResultCasted^.Value:=ResultCasted^.Value/TenPowers[high(TenPowers)]; |
| 1820 |
dec(TenPower,high(TenPowers)); |
| 1821 |
end; |
| 1822 |
if TenPower>0 then begin |
| 1823 |
ResultCasted^.Value:=ResultCasted^.Value/TenPowers[TenPower]; |
| 1824 |
end; |
| 1825 |
end; |
| 1826 |
finally |
| 1827 |
if OldFPUExceptionMask<>DtoAFPUExceptionMask then begin |
| 1828 |
SetExceptionMask(OldFPUExceptionMask); |
| 1829 |
end; |
| 1830 |
if OldFPUPrecisionMode<>DtoAFPUPrecisionMode then begin |
| 1831 |
SetPrecisionMode(OldFPUPrecisionMode); |
| 1832 |
end; |
| 1833 |
if OldFPURoundingMode<>NewFPURoundingMode then begin |
| 1834 |
SetRoundMode(OldFPURoundingMode); |
| 1835 |
end; |
| 1836 |
end; |
| 1837 |
end; |
| 1838 |
if Negative then begin |
| 1839 |
ResultCasted^.Hi:=ResultCasted^.Hi or $80000000; |
| 1840 |
end; |
| 1841 |
if assigned(OK) then begin |
| 1842 |
OK^:=true; |
| 1843 |
end; |
| 1844 |
end else begin |
| 1845 |
i:=StoredDigitPos; |
| 1846 |
GetMem(MantissaMultiplicator,SizeOf(TWords)); |
| 1847 |
GetMem(Mantissa,SizeOf(TWords)); |
| 1848 |
GetMem(Temp,SizeOf(TTemp)); |
| 1849 |
GetMem(Digits,SizeOf(TDigits)); |
| 1850 |
try |
| 1851 |
FillChar(Digits^,SizeOf(TDigits),#0); |
| 1852 |
|
| 1853 |
DigitPos:=0; |
| 1854 |
TenPower:=0; |
| 1855 |
HasDigits:=false; |
| 1856 |
Started:=false; |
| 1857 |
ExponentNegative:=false; |
| 1858 |
ExponentValue:=0; |
| 1859 |
while (i<=length(StringValue)) and (StringValue[i]='0') do begin |
| 1860 |
HasDigits:=true; |
| 1861 |
inc(i); |
| 1862 |
end; |
| 1863 |
while (i<=length(StringValue)) and (StringValue[i] in ['0'..'9']) do begin |
| 1864 |
HasDigits:=true; |
| 1865 |
Started:=true; |
| 1866 |
if DigitPos<=high(TDigits) then begin |
| 1867 |
Digits^[DigitPos]:=ord(StringValue[i])-ord('0'); |
| 1868 |
inc(DigitPos); |
| 1869 |
end; |
| 1870 |
inc(TenPower); |
| 1871 |
inc(i); |
| 1872 |
end; |
| 1873 |
if (i<=length(StringValue)) and (StringValue[i]='.') then begin |
| 1874 |
inc(i); |
| 1875 |
if not Started then begin |
| 1876 |
while (i<=length(StringValue)) and (StringValue[i]='0') do begin |
| 1877 |
HasDigits:=true; |
| 1878 |
dec(TenPower); |
| 1879 |
inc(i); |
| 1880 |
end; |
| 1881 |
end; |
| 1882 |
while (i<=length(StringValue)) and (StringValue[i] in ['0'..'9']) do begin |
| 1883 |
HasDigits:=true; |
| 1884 |
if DigitPos<=high(TDigits) then begin |
| 1885 |
Digits^[DigitPos]:=ord(StringValue[i])-ord('0'); |
| 1886 |
inc(DigitPos); |
| 1887 |
end; |
| 1888 |
inc(i); |
| 1889 |
end; |
| 1890 |
end; |
| 1891 |
if HasDigits then begin |
| 1892 |
if (i<=length(StringValue)) and (StringValue[i] in ['e','E']) then begin |
| 1893 |
inc(i); |
| 1894 |
if (i<=length(StringValue)) and (StringValue[i] in ['+','-']) then begin |
| 1895 |
ExponentNegative:=StringValue[i]='-'; |
| 1896 |
inc(i); |
| 1897 |
end; |
| 1898 |
HasDigits:=false; |
| 1899 |
while (i<=length(StringValue)) and (StringValue[i] in ['0'..'9']) do begin |
| 1900 |
ExponentValue:=(ExponentValue*10)+TPasDblStrUtilsInt32(ord(StringValue[i])-ord('0')); |
| 1901 |
HasDigits:=true; |
| 1902 |
inc(i); |
| 1903 |
end; |
| 1904 |
end; |
| 1905 |
if HasDigits then begin |
| 1906 |
if ExponentNegative then begin |
| 1907 |
dec(TenPower,ExponentValue); |
| 1908 |
end else begin |
| 1909 |
inc(TenPower,ExponentValue); |
| 1910 |
end; |
| 1911 |
|
| 1912 |
FillChar(Mantissa^,sizeof(TWords),#0); |
| 1913 |
|
| 1914 |
Bit:=WordTopBit; |
| 1915 |
StoredDigitPos:=0; |
| 1916 |
Started:=false; |
| 1917 |
TwoPower:=0; |
| 1918 |
MantissaPosition:=0; |
| 1919 |
while MantissaPosition<MantissaWords do begin |
| 1920 |
Carry:=0; |
| 1921 |
while (DigitPos>StoredDigitPos) and (Digits^[DigitPos-1]=0) do begin |
| 1922 |
dec(DigitPos); |
| 1923 |
end; |
| 1924 |
if DigitPos<=StoredDigitPos then begin |
| 1925 |
break; |
| 1926 |
end; |
| 1927 |
DigitPosBackwards:=DigitPos; |
| 1928 |
while DigitPosBackwards>StoredDigitPos do begin |
| 1929 |
dec(DigitPosBackwards); |
| 1930 |
i:=(2*Digits^[DigitPosBackwards])+Carry; |
| 1931 |
if i>=10 then begin |
| 1932 |
dec(i,10); |
| 1933 |
Carry:=1; |
| 1934 |
end else begin |
| 1935 |
Carry:=0; |
| 1936 |
end; |
| 1937 |
Digits^[DigitPosBackwards]:=i; |
| 1938 |
end; |
| 1939 |
if Carry<>0 then begin |
| 1940 |
Mantissa^[MantissaPosition]:=Mantissa^[MantissaPosition] or Bit; |
| 1941 |
Started:=true; |
| 1942 |
end; |
| 1943 |
if Started then begin |
| 1944 |
if Bit=1 then begin |
| 1945 |
Bit:=WordTopBit; |
| 1946 |
inc(MantissaPosition); |
| 1947 |
end else begin |
| 1948 |
Bit:=Bit shr 1; |
| 1949 |
end; |
| 1950 |
end else begin |
| 1951 |
dec(TwoPower); |
| 1952 |
end; |
| 1953 |
end; |
| 1954 |
inc(TwoPower,TenPower); |
| 1955 |
|
| 1956 |
if TenPower<0 then begin |
| 1957 |
for i:=0 to high(TWords)-1 do begin |
| 1958 |
MantissaMultiplicator^[i]:=$cccc; |
| 1959 |
end; |
| 1960 |
MantissaMultiplicator^[high(TWords)]:=$cccd; |
| 1961 |
ExtraTwos:=-2; |
| 1962 |
TenPower:=-TenPower; |
| 1963 |
end else if TenPower>0 then begin |
| 1964 |
MantissaMultiplicator^[0]:=$a000; |
| 1965 |
for i:=1 to high(TWords) do begin |
| 1966 |
MantissaMultiplicator^[i]:=$0000; |
| 1967 |
end; |
| 1968 |
ExtraTwos:=3; |
| 1969 |
end else begin |
| 1970 |
ExtraTwos:=0; |
| 1971 |
end; |
| 1972 |
while TenPower<>0 do begin |
| 1973 |
if (TenPower and 1)<>0 then begin |
| 1974 |
inc(TwoPower,ExtraTwos+MantissaMultiply(Mantissa,MantissaMultiplicator)); |
| 1975 |
end; |
| 1976 |
inc(ExtraTwos,ExtraTwos+MantissaMultiply(MantissaMultiplicator,MantissaMultiplicator)); |
| 1977 |
TenPower:=TenPower shr 1; |
| 1978 |
end; |
| 1979 |
|
| 1980 |
Exponent:=TwoPower; |
| 1981 |
if (Mantissa^[0] and WordTopBit)<>0 then begin |
| 1982 |
dec(Exponent); |
| 1983 |
|
| 1984 |
if (Exponent>=(2-IEEEFormatExpMax)) and (Exponent<=IEEEFormatExpMax) then begin |
| 1985 |
inc(Exponent,IEEEFormatExpMax-1); |
| 1986 |
MantissaShiftRight(Mantissa^,IEEEFormatExponent+IEEEFormatExplicit); |
| 1987 |
MantissaRound(Negative,Mantissa^,IEEEFormatBits); |
| 1988 |
if MantissaTestBit(Mantissa^,IEEEFormatExponent+IEEEFormatExplicit-1) then begin |
| 1989 |
MantissaShiftRight(Mantissa^,1); |
| 1990 |
inc(Exponent); |
| 1991 |
end; |
| 1992 |
if Exponent>=(IEEEFormatExpMax shl 1)-1 then begin |
| 1993 |
ResultCasted^.Hi:=$7ff00000; |
| 1994 |
ResultCasted^.Lo:=$00000000; |
| 1995 |
end else begin |
| 1996 |
ResultCasted^.Hi:=(((Exponent shl 4) or (Mantissa^[0] and $f)) shl 16) or Mantissa^[1]; |
| 1997 |
ResultCasted^.Lo:=(Mantissa^[2] shl 16) or Mantissa^[3]; |
| 1998 |
end; |
| 1999 |
end else if Exponent>0 then begin |
| 2000 |
ResultCasted^.Hi:=$7ff00000; |
| 2001 |
ResultCasted^.Lo:=$00000000; |
| 2002 |
end else begin |
| 2003 |
Shift:=IEEEFormatExplicit-(Exponent+(IEEEFormatExpMax-(2+IEEEFormatExponent))); |
| 2004 |
MantissaShiftRight(Mantissa^,Shift); |
| 2005 |
MantissaRound(Negative,Mantissa^,IEEEFormatBits); |
| 2006 |
if (Mantissa^[IEEEFormatOnePos] and IEEEFormatOneMask)<>0 then begin |
| 2007 |
Exponent:=1; |
| 2008 |
if IEEEFormatExplicit=0 then begin |
| 2009 |
Mantissa^[IEEEFormatOnePos]:=Mantissa^[IEEEFormatOnePos] and not IEEEFormatOneMask; |
| 2010 |
end; |
| 2011 |
Mantissa^[0]:=Mantissa^[0] or (Exponent shl (WordBitMask-IEEEFormatExponent)); |
| 2012 |
ResultCasted^.Hi:=(((Exponent shl 4) or (Mantissa^[0] and $f)) shl 16) or Mantissa^[1]; |
| 2013 |
ResultCasted^.Lo:=(Mantissa^[2] shl 16) or Mantissa^[3]; |
| 2014 |
end else begin |
| 2015 |
if MantissaIsZero(Mantissa^) then begin |
| 2016 |
ResultCasted^.Hi:=$00000000; |
| 2017 |
ResultCasted^.Lo:=$00000000; |
| 2018 |
end else begin |
| 2019 |
ResultCasted^.Hi:=(Mantissa^[0] shl 16) or Mantissa^[1]; |
| 2020 |
ResultCasted^.Lo:=(Mantissa^[2] shl 16) or Mantissa^[3]; |
| 2021 |
end; |
| 2022 |
end; |
| 2023 |
end; |
| 2024 |
end else begin |
| 2025 |
ResultCasted^.Hi:=$00000000; |
| 2026 |
ResultCasted^.Lo:=$00000000; |
| 2027 |
end; |
| 2028 |
if Negative then begin |
| 2029 |
ResultCasted^.Hi:=ResultCasted^.Hi or $80000000; |
| 2030 |
end; |
| 2031 |
if assigned(OK) then begin |
| 2032 |
OK^:=true; |
| 2033 |
end; |
| 2034 |
end; |
| 2035 |
end; |
| 2036 |
finally |
| 2037 |
FreeMem(MantissaMultiplicator); |
| 2038 |
FreeMem(Mantissa); |
| 2039 |
FreeMem(Temp); |
| 2040 |
FreeMem(Digits); |
| 2041 |
end; |
| 2042 |
end; |
| 2043 |
end; |
| 2044 |
end; |
| 2045 |
|
| 2046 |
function ConvertDoubleToString(const AValue:TPasDblStrUtilsDouble;const OutputMode:TPasDblStrUtilsOutputMode=omStandard;RequestedDigits:TPasDblStrUtilsInt32=-1):TPasDblStrUtilsString; |
| 2047 |
const SignificantMantissaSize=64; |
| 2048 |
MinimalTargetExponent=-60; |
| 2049 |
MaximalTargetExponent=-32; |
| 2050 |
ModeShortest=0; |
| 2051 |
ModeFixed=1; |
| 2052 |
ModePrecision=2; |
| 2053 |
BigNumMaxSignificantMantissaBits=3584; |
| 2054 |
BigitChunkSize=32; |
| 2055 |
BigitDoubleChunkSize=64; |
| 2056 |
BigitSize=28; |
| 2057 |
BigitMask=(1 shl BigitSize)-1; |
| 2058 |
BigNumCapacity=(BigNumMaxSignificantMantissaBits+(BigitSize-1)) div BigitSize; |
| 2059 |
type TDoubleValue=record |
| 2060 |
SignificantMantissa:TPasDblStrUtilsUInt64; |
| 2061 |
Exponent:TPasDblStrUtilsInt32; |
| 2062 |
end; |
| 2063 |
TBigNumChunk=TPasDblStrUtilsUInt32; |
| 2064 |
TBigNumDoubleChunk=TPasDblStrUtilsUInt64; |
| 2065 |
TBigNum=record |
| 2066 |
Bigits:array[0..BigNumCapacity] of TBigNumChunk; |
| 2067 |
UsedDigits:TPasDblStrUtilsInt32; |
| 2068 |
Exponent:TPasDblStrUtilsInt32; |
| 2069 |
end; |
| 2070 |
function QWordLessOrEqual(a,b:TPasDblStrUtilsUInt64):TPasDblStrUtilsBoolean; |
| 2071 |
begin |
| 2072 |
result:=(a=b) or (((a shr 32)<(b shr 32)) or (((a shr 32)=(b shr 32)) and ((a and $ffffffff)<(b and $ffffffff)))); |
| 2073 |
end; |
| 2074 |
function QWordGreaterOrEqual(a,b:TPasDblStrUtilsUInt64):TPasDblStrUtilsBoolean; |
| 2075 |
begin |
| 2076 |
result:=(a=b) or (((a shr 32)>(b shr 32)) or (((a shr 32)=(b shr 32)) and ((a and $ffffffff)>(b and $ffffffff)))); |
| 2077 |
end; |
| 2078 |
function QWordLess(a,b:TPasDblStrUtilsUInt64):TPasDblStrUtilsBoolean; |
| 2079 |
begin |
| 2080 |
result:=((a shr 32)<(b shr 32)) or (((a shr 32)=(b shr 32)) and ((a and $ffffffff)<(b and $ffffffff))); |
| 2081 |
end; |
| 2082 |
function QWordGreater(a,b:TPasDblStrUtilsUInt64):TPasDblStrUtilsBoolean; |
| 2083 |
begin |
| 2084 |
result:=((a shr 32)>(b shr 32)) or (((a shr 32)=(b shr 32)) and ((a and $ffffffff)>(b and $ffffffff))); |
| 2085 |
end; |
| 2086 |
function DoubleValue(SignificantMantissa:TPasDblStrUtilsUInt64=0;Exponent:TPasDblStrUtilsInt32=0):TDoubleValue; |
| 2087 |
begin |
| 2088 |
result.SignificantMantissa:=SignificantMantissa; |
| 2089 |
result.Exponent:=Exponent; |
| 2090 |
end; |
| 2091 |
procedure SplitDouble(Value:TPasDblStrUtilsDouble;var SignificantMantissa:TPasDblStrUtilsUInt64;var Exponent:TPasDblStrUtilsInt32); |
| 2092 |
var Casted:TPasDblStrUtilsUInt64 absolute Value; |
| 2093 |
begin |
| 2094 |
SignificantMantissa:=Casted and TPasDblStrUtilsUInt64($000fffffffffffff); |
| 2095 |
if (Casted and TPasDblStrUtilsUInt64($7ff0000000000000))<>0 then begin |
| 2096 |
inc(SignificantMantissa,TPasDblStrUtilsUInt64($0010000000000000)); |
| 2097 |
Exponent:=((Casted and TPasDblStrUtilsUInt64($7ff0000000000000)) shr 52)-($3ff+52); |
| 2098 |
end else begin |
| 2099 |
Exponent:=(-($3ff+52))+1; |
| 2100 |
end; |
| 2101 |
end; |
| 2102 |
function DoubleValueGet(Value:TPasDblStrUtilsDouble):TDoubleValue; |
| 2103 |
var SignificantMantissa:TPasDblStrUtilsUInt64; |
| 2104 |
Exponent:TPasDblStrUtilsInt32; |
| 2105 |
begin |
| 2106 |
Assert(Value>0); |
| 2107 |
SplitDouble(Value,SignificantMantissa,Exponent); |
| 2108 |
while (SignificantMantissa and TPasDblStrUtilsUInt64($0010000000000000))=0 do begin |
| 2109 |
SignificantMantissa:=SignificantMantissa shl 1; |
| 2110 |
dec(Exponent); |
| 2111 |
end; |
| 2112 |
SignificantMantissa:=SignificantMantissa shl (SignificantMantissaSize-53); |
| 2113 |
dec(Exponent,SignificantMantissaSize-53); |
| 2114 |
result.SignificantMantissa:=SignificantMantissa; |
| 2115 |
result.Exponent:=Exponent; |
| 2116 |
end; |
| 2117 |
procedure DoubleValueSubtract(var Left:TDoubleValue;const Right:TDoubleValue); |
| 2118 |
begin |
| 2119 |
Assert(Left.Exponent=Right.Exponent); |
| 2120 |
Assert(QWordGreaterOrEqual(Left.SignificantMantissa,Right.SignificantMantissa)); |
| 2121 |
dec(Left.SignificantMantissa,Right.SignificantMantissa); |
| 2122 |
end; |
| 2123 |
function DoubleValueMinus(const Left,Right:TDoubleValue):TDoubleValue; |
| 2124 |
begin |
| 2125 |
Assert(Left.Exponent=Right.Exponent); |
| 2126 |
Assert(QWordGreaterOrEqual(Left.SignificantMantissa,Right.SignificantMantissa)); |
| 2127 |
result.Exponent:=Left.Exponent; |
| 2128 |
result.SignificantMantissa:=Left.SignificantMantissa-Right.SignificantMantissa; |
| 2129 |
end; |
| 2130 |
procedure DoubleValueMuliply(var Left:TDoubleValue;const Right:TDoubleValue); |
| 2131 |
var a,b,c,d,ac,bc,ad,bd:TPasDblStrUtilsUInt64; |
| 2132 |
begin |
| 2133 |
a:=Left.SignificantMantissa shr 32; |
| 2134 |
b:=Left.SignificantMantissa and $ffffffff; |
| 2135 |
c:=Right.SignificantMantissa shr 32; |
| 2136 |
d:=Right.SignificantMantissa and $ffffffff; |
| 2137 |
ac:=a*c; |
| 2138 |
bc:=b*c; |
| 2139 |
ad:=a*d; |
| 2140 |
bd:=b*d; |
| 2141 |
inc(Left.Exponent,Right.Exponent+64); |
| 2142 |
Left.SignificantMantissa:=ac+(ad shr 32)+(bc shr 32)+(TPasDblStrUtilsUInt64(((bd shr 32)+((ad and $ffffffff)+(bc and $ffffffff)))+(TPasDblStrUtilsUInt64(1) shl 31)) shr 32); |
| 2143 |
end; |
| 2144 |
function DoubleValueMul(const Left,Right:TDoubleValue):TDoubleValue; |
| 2145 |
var a,b,c,d,ac,bc,ad,bd:TPasDblStrUtilsUInt64; |
| 2146 |
begin |
| 2147 |
a:=Left.SignificantMantissa shr 32; |
| 2148 |
b:=Left.SignificantMantissa and $ffffffff; |
| 2149 |
c:=Right.SignificantMantissa shr 32; |
| 2150 |
d:=Right.SignificantMantissa and $ffffffff; |
| 2151 |
ac:=a*c; |
| 2152 |
bc:=b*c; |
| 2153 |
ad:=a*d; |
| 2154 |
bd:=b*d; |
| 2155 |
result.Exponent:=Left.Exponent+(Right.Exponent+64); |
| 2156 |
a:=((bd shr 32)+((ad and $ffffffff)+(bc and $ffffffff)))+(TPasDblStrUtilsUInt64(1) shl 31); |
| 2157 |
result.SignificantMantissa:=ac+(ad shr 32)+(bc shr 32)+(a shr 32); |
| 2158 |
end; |
| 2159 |
procedure DoubleValueNormalize(var Value:TDoubleValue); |
| 2160 |
var SignificantMantissa:TPasDblStrUtilsUInt64; |
| 2161 |
Exponent:TPasDblStrUtilsInt32; |
| 2162 |
begin |
| 2163 |
Assert(Value.SignificantMantissa<>0); |
| 2164 |
SignificantMantissa:=Value.SignificantMantissa; |
| 2165 |
Exponent:=Value.Exponent; |
| 2166 |
while (SignificantMantissa and TPasDblStrUtilsUInt64($ffc0000000000000))=0 do begin |
| 2167 |
SignificantMantissa:=SignificantMantissa shl 10; |
| 2168 |
dec(Exponent,10); |
| 2169 |
end; |
| 2170 |
while (SignificantMantissa and TPasDblStrUtilsUInt64($8000000000000000))=0 do begin |
| 2171 |
SignificantMantissa:=SignificantMantissa shl 1; |
| 2172 |
dec(Exponent); |
| 2173 |
end; |
| 2174 |
Value.SignificantMantissa:=SignificantMantissa; |
| 2175 |
Value.Exponent:=Exponent; |
| 2176 |
end; |
| 2177 |
function DoubleValueNorm(const Value:TDoubleValue):TDoubleValue; |
| 2178 |
var SignificantMantissa:TPasDblStrUtilsUInt64; |
| 2179 |
Exponent:TPasDblStrUtilsInt32; |
| 2180 |
begin |
| 2181 |
Assert(Value.SignificantMantissa<>0); |
| 2182 |
SignificantMantissa:=Value.SignificantMantissa; |
| 2183 |
Exponent:=Value.Exponent; |
| 2184 |
while (SignificantMantissa and TPasDblStrUtilsUInt64($ffc0000000000000))=0 do begin |
| 2185 |
SignificantMantissa:=SignificantMantissa shl 10; |
| 2186 |
dec(Exponent,10); |
| 2187 |
end; |
| 2188 |
while (SignificantMantissa and TPasDblStrUtilsUInt64($8000000000000000))=0 do begin |
| 2189 |
SignificantMantissa:=SignificantMantissa shl 1; |
| 2190 |
dec(Exponent); |
| 2191 |
end; |
| 2192 |
result.SignificantMantissa:=SignificantMantissa; |
| 2193 |
result.Exponent:=Exponent; |
| 2194 |
end; |
| 2195 |
function BigNumNew:TBigNum; |
| 2196 |
begin |
| 2197 |
FillChar(result,sizeof(TBigNum),#0); |
| 2198 |
end; |
| 2199 |
procedure BigNumZero(var BigNum:TBigNum); |
| 2200 |
begin |
| 2201 |
BigNum.UsedDigits:=0; |
| 2202 |
BigNum.Exponent:=0; |
| 2203 |
end; |
| 2204 |
procedure BigNumEnsureCapacity(var BigNum:TBigNum;Size:TPasDblStrUtilsInt32); |
| 2205 |
begin |
| 2206 |
end; |
| 2207 |
procedure BigNumClamp(var BigNum:TBigNum); |
| 2208 |
begin |
| 2209 |
while (BigNum.UsedDigits>0) and (BigNum.Bigits[BigNum.UsedDigits-1]=0) do begin |
| 2210 |
dec(BigNum.UsedDigits); |
| 2211 |
end; |
| 2212 |
if BigNum.UsedDigits=0 then begin |
| 2213 |
BigNum.Exponent:=0; |
| 2214 |
end; |
| 2215 |
end; |
| 2216 |
function BigNumIsClamped(const BigNum:TBigNum):TPasDblStrUtilsBoolean; |
| 2217 |
begin |
| 2218 |
result:=(BigNum.UsedDigits=0) or (BigNum.Bigits[BigNum.UsedDigits-1]<>0); |
| 2219 |
end; |
| 2220 |
procedure BigNumAlign(var BigNum:TBigNum;const Other:TBigNum); |
| 2221 |
var ZeroDigits,i:TPasDblStrUtilsInt32; |
| 2222 |
begin |
| 2223 |
if BigNum.Exponent>Other.Exponent then begin |
| 2224 |
ZeroDigits:=BigNum.Exponent-Other.Exponent; |
| 2225 |
BigNumEnsureCapacity(BigNum,Bignum.UsedDigits+ZeroDigits); |
| 2226 |
for i:=BigNum.UsedDigits-1 downto 0 do begin |
| 2227 |
BigNum.Bigits[i+ZeroDigits]:=BigNum.Bigits[i]; |
| 2228 |
end; |
| 2229 |
for i:=0 to ZeroDigits-1 do begin |
| 2230 |
BigNum.Bigits[i]:=0; |
| 2231 |
end; |
| 2232 |
inc(BigNum.UsedDigits,ZeroDigits); |
| 2233 |
dec(BigNum.Exponent,ZeroDigits); |
| 2234 |
Assert(BigNum.UsedDigits>=0); |
| 2235 |
Assert(BigNum.Exponent>=0); |
| 2236 |
end; |
| 2237 |
end; |
| 2238 |
procedure BigNumAssignUInt16(var BigNum:TBigNum;Value:TPasDblStrUtilsUInt16); |
| 2239 |
begin |
| 2240 |
Assert(BigitSize>=(sizeof(TPasDblStrUtilsUInt16)*8)); |
| 2241 |
BigNumZero(BigNum); |
| 2242 |
if Value<>0 then begin |
| 2243 |
BigNumEnsureCapacity(BigNum,1); |
| 2244 |
BigNum.Bigits[0]:=Value; |
| 2245 |
BigNum.UsedDigits:=1; |
| 2246 |
end; |
| 2247 |
end; |
| 2248 |
procedure BigNumAssignUInt64(var BigNum:TBigNum;Value:TPasDblStrUtilsUInt64); |
| 2249 |
var i,j:TPasDblStrUtilsInt32; |
| 2250 |
begin |
| 2251 |
BigNumZero(BigNum); |
| 2252 |
if Value<>0 then begin |
| 2253 |
j:=(64 div BigitSize)+1; |
| 2254 |
BigNumEnsureCapacity(BigNum,j); |
| 2255 |
for i:=0 to j-1 do begin |
| 2256 |
BigNum.Bigits[i]:=Value and BigitMask; |
| 2257 |
Value:=Value shr BigitSize; |
| 2258 |
end; |
| 2259 |
BigNum.UsedDigits:=j; |
| 2260 |
BigNumClamp(BigNum); |
| 2261 |
end; |
| 2262 |
end; |
| 2263 |
procedure BigNumAssignBigNum(var BigNum:TBigNum;const Other:TBigNum); |
| 2264 |
begin |
| 2265 |
BigNum.Exponent:=Other.Exponent; |
| 2266 |
BigNum.Bigits:=Other.Bigits; |
| 2267 |
BigNum.UsedDigits:=Other.UsedDigits; |
| 2268 |
end; |
| 2269 |
procedure BigNumAddBigNum(var BigNum:TBigNum;const Other:TBigNum); |
| 2270 |
var Carry,Sum:TBigNumChunk; |
| 2271 |
BigitPos,i:TPasDblStrUtilsInt32; |
| 2272 |
begin |
| 2273 |
Assert(BigNumIsClamped(BigNum)); |
| 2274 |
Assert(BigNumIsClamped(Other)); |
| 2275 |
BigNumAlign(BigNum,Other); |
| 2276 |
BigNumEnsureCapacity(BigNum,BigNum.UsedDigits+Other.UsedDigits); |
| 2277 |
BigitPos:=Other.Exponent-BigNum.Exponent; |
| 2278 |
Assert(BigitPos>=0); |
| 2279 |
Carry:=0; |
| 2280 |
for i:=0 to Other.UsedDigits-1 do begin |
| 2281 |
Sum:=BigNum.Bigits[BigitPos]+Other.Bigits[i]+Carry; |
| 2282 |
BigNum.Bigits[BigitPos]:=Sum and BigitMask; |
| 2283 |
Carry:=Sum shr BigitSize; |
| 2284 |
inc(BigitPos); |
| 2285 |
end; |
| 2286 |
while Carry<>0 do begin |
| 2287 |
Sum:=BigNum.Bigits[BigitPos]+Carry; |
| 2288 |
BigNum.Bigits[BigitPos]:=Sum and BigitMask; |
| 2289 |
Carry:=Sum shr BigitSize; |
| 2290 |
inc(BigitPos); |
| 2291 |
end; |
| 2292 |
if BigNum.UsedDigits<BigitPos then begin |
| 2293 |
BigNum.UsedDigits:=BigitPos; |
| 2294 |
end; |
| 2295 |
Assert(BigNumIsClamped(BigNum)); |
| 2296 |
end; |
| 2297 |
procedure BigNumAddUInt64(var BigNum:TBigNum;const Value:TPasDblStrUtilsUInt64); |
| 2298 |
var Other:TBigNum; |
| 2299 |
begin |
| 2300 |
Other:=BigNumNew; |
| 2301 |
BigNumAssignUInt64(Other,Value); |
| 2302 |
BigNumAddBigNum(BigNum,Other); |
| 2303 |
end; |
| 2304 |
function BigNumBigitAt(const BigNum:TBigNum;Index:TPasDblStrUtilsInt32):TBigNumChunk; |
| 2305 |
begin |
| 2306 |
if (Index<BigNum.Exponent) or (Index>=(BigNum.UsedDigits+BigNum.Exponent)) then begin |
| 2307 |
result:=0; |
| 2308 |
end else begin |
| 2309 |
result:=BigNum.Bigits[Index-BigNum.Exponent]; |
| 2310 |
end; |
| 2311 |
end; |
| 2312 |
function BigNumCompare(const a,b:TBigNum):TPasDblStrUtilsInt32; |
| 2313 |
var la,lb,i,j:TPasDblStrUtilsInt32; |
| 2314 |
ba,bb:TBigNumChunk; |
| 2315 |
begin |
| 2316 |
Assert(BigNumIsClamped(a)); |
| 2317 |
Assert(BigNumIsClamped(b)); |
| 2318 |
la:=a.UsedDigits+a.Exponent; |
| 2319 |
lb:=b.UsedDigits+b.Exponent; |
| 2320 |
if la<lb then begin |
| 2321 |
result:=-1; |
| 2322 |
end else if la>lb then begin |
| 2323 |
result:=1; |
| 2324 |
end else begin |
| 2325 |
if a.Exponent<b.Exponent then begin |
| 2326 |
j:=a.Exponent; |
| 2327 |
end else begin |
| 2328 |
j:=b.Exponent; |
| 2329 |
end; |
| 2330 |
result:=0; |
| 2331 |
for i:=la-1 downto j do begin |
| 2332 |
ba:=BigNumBigItAt(a,i); |
| 2333 |
bb:=BigNumBigItAt(b,i); |
| 2334 |
if ba<bb then begin |
| 2335 |
result:=-1; |
| 2336 |
break; |
| 2337 |
end else if ba>bb then begin |
| 2338 |
result:=1; |
| 2339 |
break; |
| 2340 |
end; |
| 2341 |
end; |
| 2342 |
end; |
| 2343 |
end; |
| 2344 |
function BigNumPlusCompare(const a,b,c:TBigNum):TPasDblStrUtilsInt32; |
| 2345 |
var la,lb,lc,i,j:TPasDblStrUtilsInt32; |
| 2346 |
ba,bb,bc,br,Sum:TBigNumChunk; |
| 2347 |
begin |
| 2348 |
Assert(BigNumIsClamped(a)); |
| 2349 |
Assert(BigNumIsClamped(b)); |
| 2350 |
Assert(BigNumIsClamped(c)); |
| 2351 |
la:=a.UsedDigits+a.Exponent; |
| 2352 |
lb:=b.UsedDigits+b.Exponent; |
| 2353 |
lc:=c.UsedDigits+c.Exponent; |
| 2354 |
if la<lb then begin |
| 2355 |
result:=BigNumPlusCompare(b,a,c); |
| 2356 |
end else begin |
| 2357 |
if (la+1)<lc then begin |
| 2358 |
result:=-1; |
| 2359 |
end else if la>lc then begin |
| 2360 |
result:=1; |
| 2361 |
end else if (a.Exponent>=lb) and (la<lc) then begin |
| 2362 |
result:=-1; |
| 2363 |
end else begin |
| 2364 |
if a.Exponent<b.Exponent then begin |
| 2365 |
if a.Exponent<c.Exponent then begin |
| 2366 |
j:=a.Exponent; |
| 2367 |
end else begin |
| 2368 |
j:=c.Exponent; |
| 2369 |
end; |
| 2370 |
end else begin |
| 2371 |
if b.Exponent<c.Exponent then begin |
| 2372 |
j:=b.Exponent; |
| 2373 |
end else begin |
| 2374 |
j:=c.Exponent; |
| 2375 |
end; |
| 2376 |
end; |
| 2377 |
br:=0; |
| 2378 |
for i:=lc-1 downto j do begin |
| 2379 |
ba:=BigNumBigItAt(a,i); |
| 2380 |
bb:=BigNumBigItAt(b,i); |
| 2381 |
bc:=BigNumBigItAt(c,i); |
| 2382 |
Sum:=ba+bb; |
| 2383 |
if Sum>(bc+br) then begin |
| 2384 |
result:=1; |
| 2385 |
exit; |
| 2386 |
end else begin |
| 2387 |
br:=(bc+br)-Sum; |
| 2388 |
if br>1 then begin |
| 2389 |
result:=-1; |
| 2390 |
exit; |
| 2391 |
end; |
| 2392 |
br:=br shl BigitSize; |
| 2393 |
end; |
| 2394 |
end; |
| 2395 |
if br=0 then begin |
| 2396 |
result:=0; |
| 2397 |
end else begin |
| 2398 |
result:=-1; |
| 2399 |
end; |
| 2400 |
end; |
| 2401 |
end; |
| 2402 |
end; |
| 2403 |
procedure BigNumSubtractBigNum(var BigNum:TBigNum;const Other:TBigNum); |
| 2404 |
var Borrow,Difference:TBigNumChunk; |
| 2405 |
i,Offset:TPasDblStrUtilsInt32; |
| 2406 |
begin |
| 2407 |
Assert(BigNumIsClamped(BigNum)); |
| 2408 |
Assert(BigNumIsClamped(Other)); |
| 2409 |
Assert(BigNumCompare(Other,BigNum)<=0); |
| 2410 |
BigNumAlign(BigNum,Other); |
| 2411 |
Offset:=Other.Exponent-BigNum.Exponent; |
| 2412 |
Borrow:=0; |
| 2413 |
for i:=0 to Other.UsedDigits-1 do begin |
| 2414 |
Assert((Borrow=0) or (Borrow=1)); |
| 2415 |
Difference:=(BigNum.Bigits[i+Offset]-Other.Bigits[i])-Borrow; |
| 2416 |
BigNum.Bigits[i+Offset]:=Difference and BigitMask; |
| 2417 |
Borrow:=Difference shr (BigitChunkSize-1); |
| 2418 |
end; |
| 2419 |
i:=Other.UsedDigits; |
| 2420 |
while Borrow<>0 do begin |
| 2421 |
Difference:=BigNum.Bigits[i+Offset]-Borrow; |
| 2422 |
BigNum.Bigits[i+Offset]:=Difference and BigitMask; |
| 2423 |
Borrow:=Difference shr (BigitChunkSize-1); |
| 2424 |
inc(i); |
| 2425 |
end; |
| 2426 |
BigNumClamp(BigNum); |
| 2427 |
end; |
| 2428 |
procedure BigNumBigitsShiftLeft(var BigNum:TBigNum;Shift:TPasDblStrUtilsInt32); |
| 2429 |
var Carry,NextCarry:TBigNumChunk; |
| 2430 |
i:TPasDblStrUtilsInt32; |
| 2431 |
begin |
| 2432 |
Assert(Shift<BigitSize); |
| 2433 |
Assert(Shift>=0); |
| 2434 |
Carry:=0; |
| 2435 |
for i:=0 to BigNum.UsedDigits-1 do begin |
| 2436 |
NextCarry:=BigNum.Bigits[i] shr (BigitSize-Shift); |
| 2437 |
BigNum.Bigits[i]:=((BigNum.Bigits[i] shl Shift)+Carry) and BigitMask; |
| 2438 |
Carry:=NextCarry; |
| 2439 |
end; |
| 2440 |
if Carry<>0 then begin |
| 2441 |
BigNum.Bigits[BigNum.UsedDigits]:=Carry; |
| 2442 |
inc(BigNum.UsedDigits); |
| 2443 |
end; |
| 2444 |
end; |
| 2445 |
procedure BigNumBigitsShiftRight(var BigNum:TBigNum;Shift:TPasDblStrUtilsInt32); |
| 2446 |
var Carry,NextCarry:TBigNumChunk; |
| 2447 |
i:TPasDblStrUtilsInt32; |
| 2448 |
begin |
| 2449 |
Assert(Shift<BigitSize); |
| 2450 |
Assert(Shift>=0); |
| 2451 |
if BigNum.UsedDigits>0 then begin |
| 2452 |
Carry:=0; |
| 2453 |
for i:=BigNum.UsedDigits-1 downto 1 do begin |
| 2454 |
NextCarry:=BigNum.Bigits[i] shl (BigitSize-Shift); |
| 2455 |
BigNum.Bigits[i]:=((BigNum.Bigits[i] shr Shift)+Carry) and BigitMask; |
| 2456 |
Carry:=NextCarry; |
| 2457 |
end; |
| 2458 |
BigNum.Bigits[0]:=(BigNum.Bigits[0] shr Shift)+Carry; |
| 2459 |
end; |
| 2460 |
BigNumClamp(BigNum); |
| 2461 |
end; |
| 2462 |
procedure BignumSubtractTimes(var BigNum:TBigNum;const Other:TBigNum;Factor:TPasDblStrUtilsInt32); |
| 2463 |
var i,ExponentDiff:TPasDblStrUtilsInt32; |
| 2464 |
Borrow,Difference:TBigNumChunk; |
| 2465 |
Product,Remove:TBigNumDoubleChunk; |
| 2466 |
begin |
| 2467 |
Assert(BigNum.Exponent<=Other.Exponent); |
| 2468 |
if Factor<3 then begin |
| 2469 |
for i:=1 to Factor do begin |
| 2470 |
BigNumSubtractBignum(BigNum,Other); |
| 2471 |
end; |
| 2472 |
end else begin |
| 2473 |
Borrow:=0; |
| 2474 |
ExponentDiff:=Other.Exponent-BigNum.Exponent; |
| 2475 |
for i:=0 to Other.UsedDigits-1 do begin |
| 2476 |
Product:=TBigNumDoubleChunk(Factor)*Other.Bigits[i]; |
| 2477 |
Remove:=Borrow+Product; |
| 2478 |
Difference:=BigNum.Bigits[i+ExponentDiff]-TBigNumChunk(Remove and BigitMask); |
| 2479 |
BigNum.Bigits[i+ExponentDiff]:=Difference and BigitMask; |
| 2480 |
Borrow:=TBigNumChunk((Difference shr (BigitChunkSize-1))+(Remove shr BigitSize)); |
| 2481 |
end; |
| 2482 |
for i:=Other.UsedDigits+ExponentDiff to BigNum.UsedDigits-1 do begin |
| 2483 |
if Borrow=0 then begin |
| 2484 |
exit; |
| 2485 |
end; |
| 2486 |
Difference:=BigNum.Bigits[i]-Borrow; |
| 2487 |
BigNum.Bigits[i]:=Difference and BigitMask; |
| 2488 |
Borrow:=TBigNumChunk(Difference shr (BigitChunkSize-1)); |
| 2489 |
end; |
| 2490 |
BigNumClamp(BigNum); |
| 2491 |
end; |
| 2492 |
end; |
| 2493 |
procedure BigNumShiftLeft(var BigNum:TBigNum;Shift:TPasDblStrUtilsInt32); |
| 2494 |
begin |
| 2495 |
if BigNum.UsedDigits<>0 then begin |
| 2496 |
inc(BigNum.Exponent,Shift div BigitSize); |
| 2497 |
BignumEnsureCapacity(BigNum,BigNum.UsedDigits+1); |
| 2498 |
BigNumBigitsShiftLeft(BigNum,Shift mod BigitSize); |
| 2499 |
end; |
| 2500 |
end; |
| 2501 |
procedure BigNumShiftRight(var BigNum:TBigNum;Shift:TPasDblStrUtilsInt32); |
| 2502 |
begin |
| 2503 |
if BigNum.UsedDigits<>0 then begin |
| 2504 |
dec(BigNum.Exponent,Shift div BigitSize); |
| 2505 |
BignumEnsureCapacity(BigNum,BigNum.UsedDigits); |
| 2506 |
BigNumBigitsShiftRight(BigNum,Shift mod BigitSize); |
| 2507 |
end; |
| 2508 |
end; |
| 2509 |
procedure BigNumMultiplyByUInt32(var BigNum:TBigNum;Factor:TPasDblStrUtilsUInt16); |
| 2510 |
var Carry,Product:TPasDblStrUtilsUInt64; |
| 2511 |
i:TPasDblStrUtilsInt32; |
| 2512 |
begin |
| 2513 |
if Factor=0 then begin |
| 2514 |
BigNumZero(BigNum); |
| 2515 |
end else if Factor<>1 then begin |
| 2516 |
Assert(BigitSize<32); |
| 2517 |
Carry:=0; |
| 2518 |
for i:=0 to BigNum.UsedDigits-1 do begin |
| 2519 |
Product:=(Factor*BigNum.Bigits[i])+Carry; |
| 2520 |
BigNum.Bigits[i]:=Product and BigitMask; |
| 2521 |
Carry:=Product shr BigitSize; |
| 2522 |
end; |
| 2523 |
while Carry<>0 do begin |
| 2524 |
BigNumEnsureCapacity(BigNum,BigNum.UsedDigits+1); |
| 2525 |
BigNum.Bigits[BigNum.UsedDigits]:=Carry and BigitMask; |
| 2526 |
inc(BigNum.UsedDigits); |
| 2527 |
Carry:=Carry shr BigitSize; |
| 2528 |
end; |
| 2529 |
end; |
| 2530 |
end; |
| 2531 |
procedure BigNumMultiplyByUInt64(var BigNum:TBigNum;Factor:TPasDblStrUtilsUInt64); |
| 2532 |
var Carry,Low,High,ProductLow,ProductHigh,Tmp:TPasDblStrUtilsUInt64; |
| 2533 |
i:TPasDblStrUtilsInt32; |
| 2534 |
begin |
| 2535 |
if Factor=0 then begin |
| 2536 |
BigNumZero(BigNum); |
| 2537 |
end else if Factor<>1 then begin |
| 2538 |
Assert(BigitSize<32); |
| 2539 |
Carry:=0; |
| 2540 |
Low:=Factor and $ffffffff; |
| 2541 |
High:=Factor shr 32; |
| 2542 |
for i:=0 to BigNum.UsedDigits-1 do begin |
| 2543 |
ProductLow:=Low*BigNum.Bigits[i]; |
| 2544 |
ProductHigh:=High*BigNum.Bigits[i]; |
| 2545 |
Tmp:=(Carry and BigitMask)+ProductLow; |
| 2546 |
BigNum.Bigits[i]:=Tmp and BigitMask; |
| 2547 |
Carry:=(Carry shr BigitSize)+(Tmp shr BigitSize)+(ProductHigh shl (32-BigitSize)); |
| 2548 |
end; |
| 2549 |
while Carry<>0 do begin |
| 2550 |
BigNumEnsureCapacity(BigNum,BigNum.UsedDigits+1); |
| 2551 |
BigNum.Bigits[BigNum.UsedDigits]:=Carry and BigitMask; |
| 2552 |
inc(BigNum.UsedDigits); |
| 2553 |
Carry:=Carry shr BigitSize; |
| 2554 |
end; |
| 2555 |
end; |
| 2556 |
end; |
| 2557 |
procedure BigNumSquare(var BigNum:TBigNum); |
| 2558 |
var ProductLength,CopyOffset,i,BigitIndex1,BigitIndex2:TPasDblStrUtilsInt32; |
| 2559 |
Accumulator:TBigNumDoubleChunk; |
| 2560 |
Chunk1,Chunk2:TBigNumChunk; |
| 2561 |
begin |
| 2562 |
Assert(BigNumIsClamped(BigNum)); |
| 2563 |
ProductLength:=2*BigNum.UsedDigits; |
| 2564 |
BigNumEnsureCapacity(BigNum,ProductLength); |
| 2565 |
Assert(not ((1 shl (2*(BigItChunkSize-BigitSize)))<=BigNum.UsedDigits)); |
| 2566 |
Accumulator:=0; |
| 2567 |
CopyOffset:=BigNum.UsedDigits; |
| 2568 |
for i:=0 to BigNum.UsedDigits-1 do begin |
| 2569 |
BigNum.Bigits[i+CopyOffset]:=BigNum.Bigits[i]; |
| 2570 |
end; |
| 2571 |
for i:=0 to BigNum.UsedDigits-1 do begin |
| 2572 |
BigitIndex1:=i; |
| 2573 |
BigitIndex2:=0; |
| 2574 |
while BigitIndex1>=0 do begin |
| 2575 |
Chunk1:=BigNum.Bigits[CopyOffset+BigitIndex1]; |
| 2576 |
Chunk2:=BigNum.Bigits[CopyOffset+BigitIndex2]; |
| 2577 |
inc(Accumulator,TBigNumDoubleChunk(Chunk1)*Chunk2); |
| 2578 |
dec(BigitIndex1); |
| 2579 |
inc(BigitIndex2); |
| 2580 |
end; |
| 2581 |
BigNum.Bigits[i]:=Accumulator and BigitMask; |
| 2582 |
Accumulator:=Accumulator shr BigitSize; |
| 2583 |
end; |
| 2584 |
for i:=BigNum.UsedDigits-1 to ProductLength-1 do begin |
| 2585 |
BigitIndex1:=BigNum.UsedDigits-1; |
| 2586 |
BigitIndex2:=i-BigitIndex1; |
| 2587 |
while BigitIndex2<BigNum.UsedDigits do begin |
| 2588 |
Chunk1:=BigNum.Bigits[CopyOffset+BigitIndex1]; |
| 2589 |
Chunk2:=BigNum.Bigits[CopyOffset+BigitIndex2]; |
| 2590 |
inc(Accumulator,TBigNumDoubleChunk(Chunk1)*Chunk2); |
| 2591 |
dec(BigitIndex1); |
| 2592 |
inc(BigitIndex2); |
| 2593 |
end; |
| 2594 |
BigNum.Bigits[i]:=Accumulator and BigitMask; |
| 2595 |
Accumulator:=Accumulator shr BigitSize; |
| 2596 |
end; |
| 2597 |
Assert(Accumulator=0); |
| 2598 |
BigNum.UsedDigits:=ProductLength; |
| 2599 |
inc(BigNum.Exponent,BigNum.Exponent); |
| 2600 |
BigNumClamp(BigNum); |
| 2601 |
end; |
| 2602 |
procedure BigNumAssignPowerUInt16(var BigNum:TBigNum;Base:TPasDblStrUtilsUInt16;PowerExponent:TPasDblStrUtilsInt32); |
| 2603 |
var Shifts,BitSize,TmpBase,FinalSize,Mask:TPasDblStrUtilsInt32; |
| 2604 |
ThisValue:TPasDblStrUtilsUInt64; |
| 2605 |
DelayedMultipliciation:TPasDblStrUtilsBoolean; |
| 2606 |
begin |
| 2607 |
Assert(Base<>0); |
| 2608 |
Assert(PowerExponent>=0); |
| 2609 |
if PowerExponent=0 then begin |
| 2610 |
BigNumAssignUInt16(BigNum,1); |
| 2611 |
end else begin |
| 2612 |
BigNumZero(BigNum); |
| 2613 |
Shifts:=0; |
| 2614 |
while (Base and 1)=0 do begin |
| 2615 |
Base:=Base shr 1; |
| 2616 |
inc(Shifts); |
| 2617 |
end; |
| 2618 |
BitSize:=0; |
| 2619 |
TmpBase:=Base; |
| 2620 |
while TmpBase<>0 do begin |
| 2621 |
TmpBase:=TmpBase shr 1; |
| 2622 |
inc(BitSize); |
| 2623 |
end; |
| 2624 |
FinalSize:=BitSize*PowerExponent; |
| 2625 |
BigNumEnsureCapacity(BigNum,FinalSize); |
| 2626 |
Mask:=1; |
| 2627 |
while Mask<=PowerExponent do begin |
| 2628 |
inc(Mask,Mask); |
| 2629 |
end; |
| 2630 |
Mask:=Mask shr 2; |
| 2631 |
ThisValue:=Base; |
| 2632 |
DelayedMultipliciation:=false; |
| 2633 |
while (Mask<>0) and (ThisValue<=$ffffffff) do begin |
| 2634 |
ThisValue:=ThisValue*ThisValue; |
| 2635 |
if (PowerExponent and Mask)<>0 then begin |
| 2636 |
if (ThisValue and not ((TPasDblStrUtilsUInt64(1) shl (64-BitSize))-1))=0 then begin |
| 2637 |
ThisValue:=ThisValue*Base; |
| 2638 |
end else begin |
| 2639 |
DelayedMultipliciation:=true; |
| 2640 |
end; |
| 2641 |
end; |
| 2642 |
Mask:=Mask shr 1; |
| 2643 |
end; |
| 2644 |
BigNumAssignUInt64(BigNum,ThisValue); |
| 2645 |
if DelayedMultipliciation then begin |
| 2646 |
BigNumMultiplyByUInt32(BigNum,Base); |
| 2647 |
end; |
| 2648 |
while Mask<>0 do begin |
| 2649 |
BigNumSquare(BigNum); |
| 2650 |
if (PowerExponent and Mask)<>0 then begin |
| 2651 |
BigNumMultiplyByUInt32(BigNum,Base); |
| 2652 |
end; |
| 2653 |
Mask:=Mask shr 1; |
| 2654 |
end; |
| 2655 |
BigNumShiftLeft(BigNum,Shifts*PowerExponent); |
| 2656 |
end; |
| 2657 |
end; |
| 2658 |
function BigNumDivideModuloIntBigNum(var BigNum:TBigNum;const Other:TBigNum):TPasDblStrUtilsUInt16; |
| 2659 |
var ThisBigit,OtherBigit:TBigNumChunk; |
| 2660 |
Quotient,DivisionEstimate:TPasDblStrUtilsUInt32; |
| 2661 |
begin |
| 2662 |
Assert(BigNumIsClamped(BigNum)); |
| 2663 |
Assert(BigNumIsClamped(Other)); |
| 2664 |
Assert(Other.UsedDigits>0); |
| 2665 |
result:=0; |
| 2666 |
if (BigNum.UsedDigits+BigNum.Exponent)>=(Other.UsedDigits+Other.Exponent) then begin |
| 2667 |
BigNumAlign(BigNum,Other); |
| 2668 |
while (BigNum.UsedDigits+BigNum.Exponent)>(Other.UsedDigits+Other.Exponent) do begin |
| 2669 |
Assert(Other.Bigits[Other.UsedDigits-1]>=((1 shl BigitSize) div 16)); |
| 2670 |
inc(result,BigNum.Bigits[BigNum.UsedDigits-1]); |
| 2671 |
BigNumSubtractTimes(BigNum,Other,BigNum.Bigits[BigNum.UsedDigits-1]); |
| 2672 |
end; |
| 2673 |
Assert((BigNum.UsedDigits+BigNum.Exponent)=(Other.UsedDigits+Other.Exponent)); |
| 2674 |
ThisBigit:=BigNum.Bigits[BigNum.UsedDigits-1]; |
| 2675 |
OtherBigit:=Other.Bigits[Other.UsedDigits-1]; |
| 2676 |
if Other.UsedDigits=1 then begin |
| 2677 |
Quotient:=ThisBigit div OtherBigit; |
| 2678 |
BigNum.Bigits[BigNum.UsedDigits-1]:=ThisBigit-(OtherBigit*Quotient); |
| 2679 |
inc(result,Quotient); |
| 2680 |
BigNumClamp(BigNum); |
| 2681 |
end else begin |
| 2682 |
DivisionEstimate:=ThisBigit div (OtherBigit+1); |
| 2683 |
inc(result,DivisionEstimate); |
| 2684 |
BigNumSubtractTimes(BigNum,Other,DivisionEstimate); |
| 2685 |
if (OtherBigit*(DivisionEstimate+1))<=ThisBigit then begin |
| 2686 |
while BigNumCompare(Other,BigNum)<=0 do begin |
| 2687 |
BigNumSubtractBigNum(BigNum,Other); |
| 2688 |
inc(result); |
| 2689 |
end; |
| 2690 |
end; |
| 2691 |
end; |
| 2692 |
end; |
| 2693 |
end; |
| 2694 |
function BigNumDivideModuloInt(var BigNum:TBigNum;Divisor:TPasDblStrUtilsUInt16):TPasDblStrUtilsUInt16; |
| 2695 |
var q0,r0,q1,r1:TPasDblStrUtilsUInt64; |
| 2696 |
i:integer; |
| 2697 |
begin |
| 2698 |
Assert(BigNumIsClamped(BigNum)); |
| 2699 |
q0:=0; |
| 2700 |
for i:=BigNum.UsedDigits-1 downto 1 do begin |
| 2701 |
q1:=(BigNum.Bigits[i] div Divisor)+q0; |
| 2702 |
r1:=((BigNum.Bigits[i] mod Divisor) shl 16)+(BigNum.Bigits[i-1] shr 16); |
| 2703 |
q0:=((r1 div Divisor) shl 16); |
| 2704 |
r0:=r1 mod Divisor; |
| 2705 |
BigNum.Bigits[i]:=q1; |
| 2706 |
BigNum.Bigits[i-1]:=(r0 shl 16)+(BigNum.Bigits[i-1] and $ffff); |
| 2707 |
end; |
| 2708 |
q1:=(BigNum.Bigits[0] div Divisor)+q0; |
| 2709 |
r1:=BigNum.Bigits[0] mod Divisor; |
| 2710 |
BigNum.Bigits[0]:=q1; |
| 2711 |
result:=r1; |
| 2712 |
BigNumClamp(BigNum); |
| 2713 |
end; |
| 2714 |
function NormalizedExponent(SignificantMantissa:TPasDblStrUtilsUInt64;Exponent:TPasDblStrUtilsInt32):TPasDblStrUtilsInt32; |
| 2715 |
begin |
| 2716 |
Assert(SignificantMantissa<>0); |
| 2717 |
while (SignificantMantissa and TPasDblStrUtilsUInt64($0010000000000000))=0 do begin |
| 2718 |
SignificantMantissa:=SignificantMantissa shl 1; |
| 2719 |
dec(Exponent); |
| 2720 |
end; |
| 2721 |
result:=Exponent; |
| 2722 |
end; |
| 2723 |
function GetEstimatePower(Exponent:TPasDblStrUtilsInt32):TPasDblStrUtilsInt32; |
| 2724 |
begin |
| 2725 |
result:=TPasDblStrUtilsInt32(TPasDblStrUtilsInt64(((Exponent+52)*TPasDblStrUtilsInt64(1292913986))-$1000) shr 32)+1; // result:=System.Trunc(Math.Ceil(((Exponent+52)*0.30102999566398114)-(1e-10))); |
| 2726 |
end; |
| 2727 |
function GetEstimatePowerOf(Exponent,Radix:TPasDblStrUtilsInt32):TPasDblStrUtilsInt32; |
| 2728 |
begin |
| 2729 |
result:=TPasDblStrUtilsInt32(TPasDblStrUtilsInt64(((Exponent+52)*DoubleToStringEstimatePowerFactorTable[Radix])-$1000) shr 32)+1; // result:=System.Trunc(Math.Ceil(((Exponent+52)*(ln(2)/ln(Radix)))-(1e-10))); |
| 2730 |
end; |
| 2731 |
procedure GenerateShortestDigits(var Numerator,Denominator,DeltaMinus,DeltaPlus:TBigNum;IsEven:TPasDblStrUtilsBoolean;var Buffer:TPasDblStrUtilsString;var Len:TPasDblStrUtilsInt32); |
| 2732 |
var Digit,Compare:TPasDblStrUtilsInt32; |
| 2733 |
InDeltaRoomMinus,InDeltaRoomPlus:TPasDblStrUtilsBoolean; |
| 2734 |
begin |
| 2735 |
Len:=0; |
| 2736 |
while true do begin |
| 2737 |
Digit:=BigNumDivideModuloIntBigNum(Numerator,Denominator); |
| 2738 |
Assert((Digit>=0) and (Digit<=9)); |
| 2739 |
inc(Len); |
| 2740 |
if Len>=length(Buffer) then begin |
| 2741 |
SetLength(Buffer,Len*2); |
| 2742 |
end; |
| 2743 |
Buffer[Len]:=TPasDblStrUtilsChar(TPasDblStrUtilsUInt8(TPasDblStrUtilsUInt8(TPasDblStrUtilsChar('0'))+Digit)); |
| 2744 |
if IsEven then begin |
| 2745 |
InDeltaRoomMinus:=BigNumCompare(Numerator,DeltaMinus)<=0; |
| 2746 |
end else begin |
| 2747 |
InDeltaRoomMinus:=BigNumCompare(Numerator,DeltaMinus)<0; |
| 2748 |
end; |
| 2749 |
if IsEven then begin |
| 2750 |
InDeltaRoomPlus:=BigNumPlusCompare(Numerator,DeltaPlus,Denominator)>=0; |
| 2751 |
end else begin |
| 2752 |
InDeltaRoomPlus:=BigNumPlusCompare(Numerator,DeltaPlus,Denominator)>0; |
| 2753 |
end; |
| 2754 |
if (not InDeltaRoomMinus) and (not InDeltaRoomPlus) then begin |
| 2755 |
BigNumMultiplyByUInt32(Numerator,10); |
| 2756 |
BigNumMultiplyByUInt32(DeltaMinus,10); |
| 2757 |
BigNumMultiplyByUInt32(DeltaPlus,10); |
| 2758 |
end else if InDeltaRoomMinus and InDeltaRoomPlus then begin |
| 2759 |
Compare:=BigNumPlusCompare(Numerator,Numerator,Denominator); |
| 2760 |
if Compare<0 then begin |
| 2761 |
end else if Compare>0 then begin |
| 2762 |
Assert(Buffer[Len]<>'9'); |
| 2763 |
inc(Buffer[Len]); |
| 2764 |
end else begin |
| 2765 |
if ((ord(Buffer[Len])-ord('0')) and 1)<>0 then begin |
| 2766 |
Assert(Buffer[Len]<>'9'); |
| 2767 |
inc(Buffer[Len]); |
| 2768 |
end; |
| 2769 |
end; |
| 2770 |
exit; |
| 2771 |
end else if InDeltaRoomMinus then begin |
| 2772 |
exit; |
| 2773 |
end else begin |
| 2774 |
Assert(Buffer[Len]<>'9'); |
| 2775 |
inc(Buffer[Len]); |
| 2776 |
exit; |
| 2777 |
end; |
| 2778 |
end; |
| 2779 |
end; |
| 2780 |
procedure GenerateCountedDigits(Count:TPasDblStrUtilsInt32;var DecimalPoint:TPasDblStrUtilsInt32;var Numerator,Denominator:TBigNum;var Buffer:TPasDblStrUtilsString;var Len:TPasDblStrUtilsInt32); |
| 2781 |
var i,Digit:TPasDblStrUtilsInt32; |
| 2782 |
begin |
| 2783 |
Assert(Count>=0); |
| 2784 |
for i:=1 to Count-1 do begin |
| 2785 |
Digit:=BigNumDivideModuloIntBigNum(Numerator,Denominator); |
| 2786 |
Assert((Digit>=0) and (Digit<=9)); |
| 2787 |
inc(Len); |
| 2788 |
if Len>=length(Buffer) then begin |
| 2789 |
SetLength(Buffer,Len*2); |
| 2790 |
end; |
| 2791 |
Buffer[Len]:=TPasDblStrUtilsChar(TPasDblStrUtilsUInt8(TPasDblStrUtilsUInt8(TPasDblStrUtilsChar('0'))+Digit)); |
| 2792 |
BigNumMultiplyByUInt32(Numerator,10); |
| 2793 |
end; |
| 2794 |
Digit:=BigNumDivideModuloIntBigNum(Numerator,Denominator); |
| 2795 |
if BigNumPlusCompare(Numerator,Numerator,Denominator)>=0 then begin |
| 2796 |
inc(Digit); |
| 2797 |
end; |
| 2798 |
inc(Len); |
| 2799 |
if Len>=length(Buffer) then begin |
| 2800 |
SetLength(Buffer,Len*2); |
| 2801 |
end; |
| 2802 |
Buffer[Len]:=TPasDblStrUtilsChar(TPasDblStrUtilsUInt8(TPasDblStrUtilsUInt8(TPasDblStrUtilsChar('0'))+Digit)); |
| 2803 |
for i:=Len downto 2 do begin |
| 2804 |
if ord(Buffer[i])<>(ord('0')+10) then begin |
| 2805 |
break; |
| 2806 |
end; |
| 2807 |
Buffer[i]:='0'; |
| 2808 |
inc(Buffer[i-1]); |
| 2809 |
end; |
| 2810 |
if ord(Buffer[1])=(ord('0')+10) then begin |
| 2811 |
Buffer[1]:='1'; |
| 2812 |
inc(DecimalPoint); |
| 2813 |
end; |
| 2814 |
end; |
| 2815 |
procedure GenerateFixedDigits(RequestedDigits:TPasDblStrUtilsInt32;var DecimalPoint:TPasDblStrUtilsInt32;var Numerator,Denominator:TBigNum;var Buffer:TPasDblStrUtilsString;var Len:TPasDblStrUtilsInt32); |
| 2816 |
begin |
| 2817 |
if (-DecimalPoint)>RequestedDigits then begin |
| 2818 |
DecimalPoint:=-RequestedDigits; |
| 2819 |
Len:=0; |
| 2820 |
end else if (-DecimalPoint)=RequestedDigits then begin |
| 2821 |
Assert(DecimalPoint=(-RequestedDigits)); |
| 2822 |
BigNumMultiplyByUInt32(Denominator,10); |
| 2823 |
if BigNumPlusCompare(Numerator,Numerator,Denominator)>=0 then begin |
| 2824 |
Buffer:='1'; |
| 2825 |
Len:=1; |
| 2826 |
end else begin |
| 2827 |
Len:=0; |
| 2828 |
end; |
| 2829 |
end else begin |
| 2830 |
GenerateCountedDigits(DecimalPoint+RequestedDigits,DecimalPoint,Numerator,Denominator,Buffer,Len); |
| 2831 |
end; |
| 2832 |
end; |
| 2833 |
procedure FixupMultiplyBase(EstimatedPower:TPasDblStrUtilsInt32;IsEven:TPasDblStrUtilsBoolean;var DecimalPoint:TPasDblStrUtilsInt32;var Numerator,Denominator,DeltaMinus,DeltaPlus:TBigNum;Base:TPasDblStrUtilsInt32); |
| 2834 |
var InRange:TPasDblStrUtilsBoolean; |
| 2835 |
begin |
| 2836 |
if IsEven then begin |
| 2837 |
InRange:=BigNumPlusCompare(Numerator,DeltaPlus,Denominator)>=0; |
| 2838 |
end else begin |
| 2839 |
InRange:=BigNumPlusCompare(Numerator,DeltaPlus,Denominator)>0; |
| 2840 |
end; |
| 2841 |
if InRange then begin |
| 2842 |
DecimalPoint:=EstimatedPower+1; |
| 2843 |
end else begin |
| 2844 |
DecimalPoint:=EstimatedPower; |
| 2845 |
BigNumMultiplyByUInt32(Numerator,Base); |
| 2846 |
if BigNumCompare(DeltaMinus,DeltaPlus)=0 then begin |
| 2847 |
BigNumMultiplyByUInt32(DeltaMinus,Base); |
| 2848 |
BigNumAssignBigNum(DeltaPlus,DeltaMinus); |
| 2849 |
end else begin |
| 2850 |
BigNumMultiplyByUInt32(DeltaMinus,Base); |
| 2851 |
BigNumMultiplyByUInt32(DeltaPlus,Base); |
| 2852 |
end; |
| 2853 |
end; |
| 2854 |
end; |
| 2855 |
procedure InitialScaledStartValuesPositiveExponent(Casted,SignificantMantissa:TPasDblStrUtilsUInt64;Exponent:TPasDblStrUtilsInt32;EstimatedPower:TPasDblStrUtilsInt32;NeedBoundaryDeltas:TPasDblStrUtilsBoolean;var Numerator,Denominator,DeltaMinus,DeltaPlus:TBigNum;Base:TPasDblStrUtilsInt32); |
| 2856 |
begin |
| 2857 |
Assert(EstimatedPower>=0); |
| 2858 |
|
| 2859 |
BigNumAssignUInt64(Numerator,SignificantMantissa); |
| 2860 |
BigNumShiftLeft(Numerator,Exponent); |
| 2861 |
BigNumAssignPowerUInt16(Denominator,Base,EstimatedPower); |
| 2862 |
|
| 2863 |
if NeedBoundaryDeltas then begin |
| 2864 |
BigNumShiftLeft(Numerator,1); |
| 2865 |
BigNumShiftLeft(Denominator,1); |
| 2866 |
|
| 2867 |
BigNumAssignUInt16(DeltaPlus,1); |
| 2868 |
BigNumShiftLeft(DeltaPlus,Exponent); |
| 2869 |
|
| 2870 |
BigNumAssignUInt16(DeltaMinus,1); |
| 2871 |
BigNumShiftLeft(DeltaMinus,Exponent); |
| 2872 |
|
| 2873 |
if (Casted and TPasDblStrUtilsUInt64($000fffffffffffff))=0 then begin |
| 2874 |
BigNumShiftLeft(Numerator,1); |
| 2875 |
BigNumShiftLeft(Denominator,1); |
| 2876 |
BigNumShiftLeft(DeltaPlus,1); |
| 2877 |
end; |
| 2878 |
end; |
| 2879 |
end; |
| 2880 |
procedure InitialScaledStartValuesNegativeExponentPositivePower(Casted,SignificantMantissa:TPasDblStrUtilsUInt64;Exponent:TPasDblStrUtilsInt32;EstimatedPower:TPasDblStrUtilsInt32;NeedBoundaryDeltas:TPasDblStrUtilsBoolean;var Numerator,Denominator,DeltaMinus,DeltaPlus:TBigNum;Base:TPasDblStrUtilsInt32); |
| 2881 |
begin |
| 2882 |
BigNumAssignUInt64(Numerator,SignificantMantissa); |
| 2883 |
BigNumAssignPowerUInt16(Denominator,Base,EstimatedPower); |
| 2884 |
BigNumShiftLeft(Denominator,-Exponent); |
| 2885 |
|
| 2886 |
if NeedBoundaryDeltas then begin |
| 2887 |
BigNumShiftLeft(Numerator,1); |
| 2888 |
BigNumShiftLeft(Denominator,1); |
| 2889 |
|
| 2890 |
BigNumAssignUInt16(DeltaPlus,1); |
| 2891 |
BigNumAssignUInt16(DeltaMinus,1); |
| 2892 |
|
| 2893 |
if (Casted and TPasDblStrUtilsUInt64($000fffffffffffff))=0 then begin |
| 2894 |
BigNumShiftLeft(Numerator,1); |
| 2895 |
BigNumShiftLeft(Denominator,1); |
| 2896 |
BigNumShiftLeft(DeltaPlus,1); |
| 2897 |
end; |
| 2898 |
end; |
| 2899 |
end; |
| 2900 |
procedure InitialScaledStartValuesNegativeExponentNegativePower(Casted,SignificantMantissa:TPasDblStrUtilsUInt64;Exponent:TPasDblStrUtilsInt32;EstimatedPower:TPasDblStrUtilsInt32;NeedBoundaryDeltas:TPasDblStrUtilsBoolean;var Numerator,Denominator,DeltaMinus,DeltaPlus:TBigNum;Base:TPasDblStrUtilsInt32); |
| 2901 |
begin |
| 2902 |
BigNumAssignPowerUInt16(Numerator,Base,-EstimatedPower); |
| 2903 |
if NeedBoundaryDeltas then begin |
| 2904 |
BigNumAssignBigNum(DeltaPlus,Numerator); |
| 2905 |
BigNumAssignBigNum(DeltaMinus,Numerator); |
| 2906 |
end; |
| 2907 |
BigNumMultiplyByUInt64(Numerator,SignificantMantissa); |
| 2908 |
|
| 2909 |
BigNumAssignUInt16(Denominator,1); |
| 2910 |
BigNumShiftLeft(Denominator,-Exponent); |
| 2911 |
|
| 2912 |
if NeedBoundaryDeltas then begin |
| 2913 |
BigNumShiftLeft(Numerator,1); |
| 2914 |
BigNumShiftLeft(Denominator,1); |
| 2915 |
if ((Casted and TPasDblStrUtilsUInt64($000fffffffffffff))=0) and ((Casted and TPasDblStrUtilsUInt64($7ff0000000000000))<>TPasDblStrUtilsUInt64($0010000000000000)) then begin |
| 2916 |
BigNumShiftLeft(Numerator,1); |
| 2917 |
BigNumShiftLeft(Denominator,1); |
| 2918 |
BigNumShiftLeft(DeltaPlus,1); |
| 2919 |
end; |
| 2920 |
end; |
| 2921 |
end; |
| 2922 |
procedure InitialScaledStartValues(Casted,SignificantMantissa:TPasDblStrUtilsUInt64;Exponent:TPasDblStrUtilsInt32;EstimatedPower:TPasDblStrUtilsInt32;NeedBoundaryDeltas:TPasDblStrUtilsBoolean;var Numerator,Denominator,DeltaMinus,DeltaPlus:TBigNum;Base:TPasDblStrUtilsInt32); |
| 2923 |
begin |
| 2924 |
if Exponent>=0 then begin |
| 2925 |
InitialScaledStartValuesPositiveExponent(Casted,SignificantMantissa,Exponent,EstimatedPower,NeedBoundaryDeltas,Numerator,Denominator,DeltaMinus,DeltaPlus,Base); |
| 2926 |
end else if EstimatedPower>=0 then begin |
| 2927 |
InitialScaledStartValuesNegativeExponentPositivePower(Casted,SignificantMantissa,Exponent,EstimatedPower,NeedBoundaryDeltas,Numerator,Denominator,DeltaMinus,DeltaPlus,Base); |
| 2928 |
end else begin |
| 2929 |
InitialScaledStartValuesNegativeExponentNegativePower(Casted,SignificantMantissa,Exponent,EstimatedPower,NeedBoundaryDeltas,Numerator,Denominator,DeltaMinus,DeltaPlus,Base); |
| 2930 |
end; |
| 2931 |
end; |
| 2932 |
procedure DoubleToDecimal(Value:TPasDblStrUtilsDouble;Mode,RequestedDigits:TPasDblStrUtilsInt32;var Buffer:TPasDblStrUtilsString;var Len,DecimalPoint:TPasDblStrUtilsInt32); |
| 2933 |
var Casted:TPasDblStrUtilsUInt64 absolute Value; |
| 2934 |
SignificantMantissa:TPasDblStrUtilsUInt64; |
| 2935 |
Exponent,EstimatedPower:TPasDblStrUtilsInt32; |
| 2936 |
Numerator,Denominator,DeltaMinus,DeltaPlus:TBigNum; |
| 2937 |
IsEven,NeedBoundaryDeltas:TPasDblStrUtilsBoolean; |
| 2938 |
begin |
| 2939 |
Assert(Value>0); |
| 2940 |
Assert(IsFinite(Value)); |
| 2941 |
SplitDouble(Value,SignificantMantissa,Exponent); |
| 2942 |
IsEven:=(SignificantMantissa and 1)=0; |
| 2943 |
EstimatedPower:=GetEstimatePower(NormalizedExponent(SignificantMantissa,Exponent)); |
| 2944 |
if (Mode=ModeFixed) and (((-EstimatedPower)-1)>RequestedDigits) then begin |
| 2945 |
Buffer:=''; |
| 2946 |
Len:=0; |
| 2947 |
DecimalPoint:=-RequestedDigits; |
| 2948 |
end else begin |
| 2949 |
Assert(BigNumMaxSignificantMantissaBits>=(324*4)); |
| 2950 |
NeedBoundaryDeltas:=Mode=ModeShortest; |
| 2951 |
InitialScaledStartValues(Casted,SignificantMantissa,Exponent,EstimatedPower,NeedBoundaryDeltas,Numerator,Denominator,DeltaMinus,DeltaPlus,10); |
| 2952 |
FixupMultiplyBase(EstimatedPower,IsEven,DecimalPoint,Numerator,Denominator,DeltaMinus,DeltaPlus,10); |
| 2953 |
case Mode of |
| 2954 |
ModeShortest:begin |
| 2955 |
GenerateShortestDigits(Numerator,Denominator,DeltaMinus,DeltaPlus,IsEven,Buffer,Len); |
| 2956 |
end; |
| 2957 |
ModeFixed:begin |
| 2958 |
GenerateFixedDigits(RequestedDigits,DecimalPoint,Numerator,Denominator,Buffer,Len); |
| 2959 |
end; |
| 2960 |
else {ModePrecision:}begin |
| 2961 |
GenerateCountedDigits(RequestedDigits,DecimalPoint,Numerator,Denominator,Buffer,Len); |
| 2962 |
end; |
| 2963 |
end; |
| 2964 |
end; |
| 2965 |
end; |
| 2966 |
procedure GenerateRadixDigits(var Numerator,Denominator,DeltaMinus,DeltaPlus:TBigNum;IsEven:TPasDblStrUtilsBoolean;var Buffer:TPasDblStrUtilsString;var Len:TPasDblStrUtilsInt32;Radix:TPasDblStrUtilsInt32); |
| 2967 |
const Base36:array[0..36] of TPasDblStrUtilsChar='0123456789abcdefghijklmnopqrstuvwxyz{'; |
| 2968 |
var Digit,Compare,MaxDigit:TPasDblStrUtilsInt32; |
| 2969 |
InDeltaRoomMinus,InDeltaRoomPlus:TPasDblStrUtilsBoolean; |
| 2970 |
function ValueOf(c:TPasDblStrUtilsChar):TPasDblStrUtilsInt32; |
| 2971 |
begin |
| 2972 |
case c of |
| 2973 |
'0'..'9':begin |
| 2974 |
result:=ord(c)-ord('0'); |
| 2975 |
end; |
| 2976 |
else begin |
| 2977 |
result:=(ord(c)-ord('a'))+$a; |
| 2978 |
end; |
| 2979 |
end; |
| 2980 |
end; |
| 2981 |
begin |
| 2982 |
Len:=0; |
| 2983 |
MaxDigit:=Radix-1; |
| 2984 |
while true do begin |
| 2985 |
Digit:=BigNumDivideModuloIntBigNum(Numerator,Denominator); |
| 2986 |
Assert((Digit>=0) and (Digit<=MaxDigit)); |
| 2987 |
inc(Len); |
| 2988 |
if Len>=length(Buffer) then begin |
| 2989 |
SetLength(Buffer,Len*2); |
| 2990 |
end; |
| 2991 |
Buffer[Len]:=Base36[Digit]; |
| 2992 |
BigNumClamp(Numerator); |
| 2993 |
BigNumClamp(DeltaMinus); |
| 2994 |
BigNumClamp(DeltaPlus); |
| 2995 |
if IsEven then begin |
| 2996 |
InDeltaRoomMinus:=BigNumCompare(Numerator,DeltaMinus)<=0; |
| 2997 |
end else begin |
| 2998 |
InDeltaRoomMinus:=BigNumCompare(Numerator,DeltaMinus)<0; |
| 2999 |
end; |
| 3000 |
if IsEven then begin |
| 3001 |
InDeltaRoomPlus:=BigNumPlusCompare(Numerator,DeltaPlus,Denominator)>=0; |
| 3002 |
end else begin |
| 3003 |
InDeltaRoomPlus:=BigNumPlusCompare(Numerator,DeltaPlus,Denominator)>0; |
| 3004 |
end; |
| 3005 |
if (not InDeltaRoomMinus) and (not InDeltaRoomPlus) then begin |
| 3006 |
BigNumMultiplyByUInt32(Numerator,Radix); |
| 3007 |
BigNumMultiplyByUInt32(DeltaMinus,Radix); |
| 3008 |
BigNumMultiplyByUInt32(DeltaPlus,Radix); |
| 3009 |
end else if InDeltaRoomMinus and InDeltaRoomPlus then begin |
| 3010 |
Compare:=BigNumPlusCompare(Numerator,Numerator,Denominator); |
| 3011 |
if Compare<0 then begin |
| 3012 |
end else if Compare>0 then begin |
| 3013 |
Assert(ValueOf(Buffer[Len])<>MaxDigit); |
| 3014 |
Buffer[Len]:=Base36[ValueOf(Buffer[Len])+1]; |
| 3015 |
end else begin |
| 3016 |
if (ValueOf(Buffer[Len]) and 1)<>0 then begin |
| 3017 |
Assert(ValueOf(Buffer[Len])<>MaxDigit); |
| 3018 |
Buffer[Len]:=Base36[ValueOf(Buffer[Len])+1]; |
| 3019 |
end; |
| 3020 |
end; |
| 3021 |
exit; |
| 3022 |
end else if InDeltaRoomMinus then begin |
| 3023 |
exit; |
| 3024 |
end else begin |
| 3025 |
Assert(ValueOf(Buffer[Len])<>MaxDigit); |
| 3026 |
Buffer[Len]:=Base36[ValueOf(Buffer[Len])+1]; |
| 3027 |
exit; |
| 3028 |
end; |
| 3029 |
end; |
| 3030 |
end; |
| 3031 |
procedure DoubleToRadix(Value:TPasDblStrUtilsDouble;Radix:TPasDblStrUtilsInt32;var Buffer:TPasDblStrUtilsString;var Len,DecimalPoint:TPasDblStrUtilsInt32); |
| 3032 |
var Casted:TPasDblStrUtilsUInt64 absolute Value; |
| 3033 |
SignificantMantissa:TPasDblStrUtilsUInt64; |
| 3034 |
Exponent,EstimatedPower:TPasDblStrUtilsInt32; |
| 3035 |
Numerator,Denominator,DeltaMinus,DeltaPlus:TBigNum; |
| 3036 |
IsEven,NeedBoundaryDeltas:TPasDblStrUtilsBoolean; |
| 3037 |
begin |
| 3038 |
Assert(Value>0); |
| 3039 |
Assert(IsFinite(Value)); |
| 3040 |
SplitDouble(Value,SignificantMantissa,Exponent); |
| 3041 |
IsEven:=(SignificantMantissa and 1)=0; |
| 3042 |
EstimatedPower:=GetEstimatePowerOf(NormalizedExponent(SignificantMantissa,Exponent),Radix); |
| 3043 |
Assert(BigNumMaxSignificantMantissaBits>=(324*4)); |
| 3044 |
NeedBoundaryDeltas:=true; |
| 3045 |
InitialScaledStartValues(Casted,SignificantMantissa,Exponent,EstimatedPower,NeedBoundaryDeltas,Numerator,Denominator,DeltaMinus,DeltaPlus,Radix); |
| 3046 |
FixupMultiplyBase(EstimatedPower,IsEven,DecimalPoint,Numerator,Denominator,DeltaMinus,DeltaPlus,Radix); |
| 3047 |
GenerateRadixDigits(Numerator,Denominator,DeltaMinus,DeltaPlus,IsEven,Buffer,Len,Radix); |
| 3048 |
end; |
| 3049 |
{$warnings off} |
| 3050 |
procedure FastDoubleToRadix(v:TPasDblStrUtilsDouble;Radix:TPasDblStrUtilsInt32;var Buffer:TPasDblStrUtilsString;var Len,DecimalPoint:TPasDblStrUtilsInt32); |
| 3051 |
const Base36:array[0..35] of TPasDblStrUtilsChar='0123456789abcdefghijklmnopqrstuvwxyz'; |
| 3052 |
DtoAFPUExceptionMask:TFPUExceptionMask=[exInvalidOp,exDenormalized,exZeroDivide,exOverflow,exUnderflow,exPrecision]; |
| 3053 |
DtoAFPUPrecisionMode:TFPUPrecisionMode=pmDOUBLE; |
| 3054 |
DtoAFPURoundingMode:TFPURoundingMode=rmNEAREST; |
| 3055 |
var IntPart,FracPart,Old,Epsilon:TPasDblStrUtilsDouble; |
| 3056 |
Digit,i,j:TPasDblStrUtilsInt32; |
| 3057 |
TempBuffer:TPasDblStrUtilsString; |
| 3058 |
OldFPUExceptionMask:TFPUExceptionMask; |
| 3059 |
OldFPUPrecisionMode:TFPUPrecisionMode; |
| 3060 |
OldFPURoundingMode:TFPURoundingMode; |
| 3061 |
IntPart64:TPasDblStrUtilsInt64; |
| 3062 |
begin |
| 3063 |
if (Radix<2) or (Radix>36) then begin |
| 3064 |
result:=''; |
| 3065 |
end else begin |
| 3066 |
OldFPUExceptionMask:=GetExceptionMask; |
| 3067 |
OldFPUPrecisionMode:=GetPrecisionMode; |
| 3068 |
OldFPURoundingMode:=GetRoundMode; |
| 3069 |
try |
| 3070 |
if OldFPUExceptionMask<>DtoAFPUExceptionMask then begin |
| 3071 |
SetExceptionMask(DtoAFPUExceptionMask); |
| 3072 |
end; |
| 3073 |
if OldFPUPrecisionMode<>DtoAFPUPrecisionMode then begin |
| 3074 |
SetPrecisionMode(DtoAFPUPrecisionMode); |
| 3075 |
end; |
| 3076 |
if OldFPURoundingMode<>DtoAFPURoundingMode then begin |
| 3077 |
SetRoundMode(DtoAFPURoundingMode); |
| 3078 |
end; |
| 3079 |
try |
| 3080 |
TempBuffer:=''; |
| 3081 |
IntPart:=System.Int(v); |
| 3082 |
FracPart:=System.Frac(v); |
| 3083 |
if IntPart=0 then begin |
| 3084 |
result:='0'; |
| 3085 |
end else begin |
| 3086 |
if IntPart<4294967295.0 then begin |
| 3087 |
IntPart64:=trunc(IntPart); |
| 3088 |
while IntPart64>0 do begin |
| 3089 |
Digit:=IntPart64 mod Radix; |
| 3090 |
Assert((Digit>=0) and (Digit<Radix)); |
| 3091 |
IntPart64:=IntPart64 div Radix; |
| 3092 |
inc(Len); |
| 3093 |
if Len>=length(TempBuffer) then begin |
| 3094 |
SetLength(TempBuffer,Len*2); |
| 3095 |
end; |
| 3096 |
TempBuffer[Len]:=Base36[Digit]; |
| 3097 |
end; |
| 3098 |
end else begin |
| 3099 |
while IntPart>0 do begin |
| 3100 |
Old:=IntPart; |
| 3101 |
IntPart:=System.Int(IntPart/Radix); |
| 3102 |
Digit:=trunc(Old-(IntPart*Radix)); |
| 3103 |
Assert((Digit>=0) and (Digit<Radix)); |
| 3104 |
inc(Len); |
| 3105 |
if Len>=length(TempBuffer) then begin |
| 3106 |
SetLength(TempBuffer,Len*2); |
| 3107 |
end; |
| 3108 |
TempBuffer[Len]:=Base36[Digit]; |
| 3109 |
end; |
| 3110 |
end; |
| 3111 |
SetLength(Buffer,Len); |
| 3112 |
j:=1; |
| 3113 |
for i:=Len downto 1 do begin |
| 3114 |
Buffer[j]:=TempBuffer[i]; |
| 3115 |
inc(j); |
| 3116 |
end; |
| 3117 |
end; |
| 3118 |
if FracPart<>0 then begin |
| 3119 |
inc(Len); |
| 3120 |
if Len>=length(Buffer) then begin |
| 3121 |
SetLength(Buffer,Len*2); |
| 3122 |
end; |
| 3123 |
Buffer[Len]:='.'; |
| 3124 |
Epsilon:=0.001/Radix; |
| 3125 |
while (FracPart>=Epsilon) and (Len<32) do begin |
| 3126 |
FracPart:=FracPart*Radix; |
| 3127 |
Digit:=trunc(FracPart); |
| 3128 |
FracPart:=System.Frac(FracPart); |
| 3129 |
Assert((Digit>=0) and (Digit<Radix)); |
| 3130 |
inc(Len); |
| 3131 |
if Len>=length(Buffer) then begin |
| 3132 |
SetLength(Buffer,Len*2); |
| 3133 |
end; |
| 3134 |
Buffer[Len]:=Base36[Digit]; |
| 3135 |
end; |
| 3136 |
end; |
| 3137 |
finally |
| 3138 |
TempBuffer:=''; |
| 3139 |
end; |
| 3140 |
finally |
| 3141 |
if OldFPUExceptionMask<>DtoAFPUExceptionMask then begin |
| 3142 |
SetExceptionMask(OldFPUExceptionMask); |
| 3143 |
end; |
| 3144 |
if OldFPUPrecisionMode<>DtoAFPUPrecisionMode then begin |
| 3145 |
SetPrecisionMode(OldFPUPrecisionMode); |
| 3146 |
end; |
| 3147 |
if OldFPURoundingMode<>DtoAFPURoundingMode then begin |
| 3148 |
SetRoundMode(OldFPURoundingMode); |
| 3149 |
end; |
| 3150 |
end; |
| 3151 |
end; |
| 3152 |
end; |
| 3153 |
{$warnings on} |
| 3154 |
function GetCachedPowerForBinaryExponentRange(MinExponent,MaxExponent:TPasDblStrUtilsInt32;var Power:TDoubleValue;var DecimalExponent:TPasDblStrUtilsInt32):TPasDblStrUtilsBoolean; |
| 3155 |
var Index:TPasDblStrUtilsInt32; |
| 3156 |
begin |
| 3157 |
result:=false; |
| 3158 |
if (low(DoubleToStringPowerOfTenBinaryExponentTable)<=MinExponent) and (MinExponent<=high(DoubleToStringPowerOfTenBinaryExponentTable)) then begin |
| 3159 |
Index:=DoubleToStringPowerOfTenBinaryExponentTable[MinExponent]; |
| 3160 |
if ((Index>=0) and (Index<length(DoubleToStringPowerOfTenTable))) and ((MinExponent<=DoubleToStringPowerOfTenTable[Index,1]) and (DoubleToStringPowerOfTenTable[Index,1]<=MaxExponent)) then begin |
| 3161 |
Power.SignificantMantissa:=DoubleToStringPowerOfTenTable[Index,0]; |
| 3162 |
Power.Exponent:=DoubleToStringPowerOfTenTable[Index,1]; |
| 3163 |
DecimalExponent:=DoubleToStringPowerOfTenTable[Index,2]; |
| 3164 |
result:=true; |
| 3165 |
end; |
| 3166 |
end; |
| 3167 |
end; |
| 3168 |
function GetCachedPowerForDecimalExponent(RequestedExponent:TPasDblStrUtilsInt32;var Power:TDoubleValue;var FoundExponent:TPasDblStrUtilsInt32):TPasDblStrUtilsBoolean; |
| 3169 |
var Index:TPasDblStrUtilsInt32; |
| 3170 |
begin |
| 3171 |
result:=false; |
| 3172 |
if (low(DoubleToStringPowerOfTenDecimalExponentTable)<=RequestedExponent) and (RequestedExponent<=high(DoubleToStringPowerOfTenDecimalExponentTable)) then begin |
| 3173 |
Index:=DoubleToStringPowerOfTenDecimalExponentTable[RequestedExponent]; |
| 3174 |
if (Index>=0) and (Index<length(DoubleToStringPowerOfTenTable)) then begin |
| 3175 |
Power.SignificantMantissa:=DoubleToStringPowerOfTenTable[Index,0]; |
| 3176 |
Power.Exponent:=DoubleToStringPowerOfTenTable[Index,1]; |
| 3177 |
FoundExponent:=DoubleToStringPowerOfTenTable[Index,2]; |
| 3178 |
result:=true; |
| 3179 |
end; |
| 3180 |
end; |
| 3181 |
end; |
| 3182 |
function RoundWeed(var Buffer:TPasDblStrUtilsString;Len:TPasDblStrUtilsInt32;DistanceTooHighW,UnsafeInterval,Rest,TenCapacity,UnitValue:TPasDblStrUtilsUInt64):TPasDblStrUtilsBoolean; |
| 3183 |
var SmallDistance,BigDistance:TPasDblStrUtilsUInt64; |
| 3184 |
begin |
| 3185 |
SmallDistance:=DistanceTooHighW-UnitValue; |
| 3186 |
BigDistance:=DistanceTooHighW+UnitValue; |
| 3187 |
Assert(QWordLessOrEqual(Rest,UnsafeInterval)); |
| 3188 |
while (QWordLess(Rest,SmallDistance) and (QWordGreaterOrEqual(UnsafeInterval-Rest,TenCapacity))) and (QWordLess(Rest+TenCapacity,SmallDistance) or QWordGreaterOrEqual(SmallDistance-Rest,((Rest+TenCapacity)-SmallDistance))) do begin |
| 3189 |
dec(Buffer[Len]); |
| 3190 |
inc(Rest,TenCapacity); |
| 3191 |
end; |
| 3192 |
if ((QWordLess(Rest,BigDistance) and QWordGreaterOrEqual(UnsafeInterval-Rest,TenCapacity)) and (QWordLess(Rest+TenCapacity,BigDistance) or QWordGreater(BigDistance-Rest,((Rest+TenCapacity)-BigDistance)))) then begin |
| 3193 |
result:=false; |
| 3194 |
end else begin |
| 3195 |
result:=(QWordLessOrEqual(2*UnitValue,Rest) and QWordLessOrEqual(Rest,UnsafeInterval-(4*UnitValue))); |
| 3196 |
end; |
| 3197 |
end; |
| 3198 |
function RoundWeedCounted(var Buffer:TPasDblStrUtilsString;Len:TPasDblStrUtilsInt32;Rest,TenCapacity,UnitValue:TPasDblStrUtilsUInt64;var Capacity:TPasDblStrUtilsInt32):TPasDblStrUtilsBoolean; |
| 3199 |
var i:TPasDblStrUtilsInt32; |
| 3200 |
begin |
| 3201 |
Assert(QWordLess(Rest,TenCapacity)); |
| 3202 |
result:=false; |
| 3203 |
if QWordGreater(TenCapacity-UnitValue,UnitValue) then begin |
| 3204 |
result:=QWordGreater(TenCapacity-Rest,Rest) and QWordGreaterOrEqual(TenCapacity-(2*Rest),2*UnitValue); |
| 3205 |
if not result then begin |
| 3206 |
result:=QWordGreater(Rest,UnitValue) and QWordLessOrEqual(TenCapacity-(Rest-UnitValue),Rest-UnitValue); |
| 3207 |
if result then begin |
| 3208 |
inc(Buffer[Len]); |
| 3209 |
for i:=Len downto 2 do begin |
| 3210 |
if ord(Buffer[i])<>(ord('0')+10) then begin |
| 3211 |
break; |
| 3212 |
end; |
| 3213 |
Buffer[i]:='0'; |
| 3214 |
inc(Buffer[i-1]); |
| 3215 |
end; |
| 3216 |
end; |
| 3217 |
if ord(Buffer[1])=(ord('0')+10) then begin |
| 3218 |
Buffer[1]:='1'; |
| 3219 |
inc(Capacity); |
| 3220 |
end; |
| 3221 |
end; |
| 3222 |
end; |
| 3223 |
end; |
| 3224 |
function BiggestPowerTen(Number:TPasDblStrUtilsUInt32;NumberBits:TPasDblStrUtilsInt32;var Power:TPasDblStrUtilsUInt32;var Exponent:TPasDblStrUtilsInt32):TPasDblStrUtilsBoolean; |
| 3225 |
label c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11; |
| 3226 |
begin |
| 3227 |
result:=true; |
| 3228 |
case NumberBits of |
| 3229 |
30,31,32:begin |
| 3230 |
c1: |
| 3231 |
if 1000000000<=Number then begin |
| 3232 |
Power:=1000000000; |
| 3233 |
Exponent:=9; |
| 3234 |
end else begin |
| 3235 |
goto c2; |
| 3236 |
end; |
| 3237 |
end; |
| 3238 |
27,28,29:begin |
| 3239 |
c2: |
| 3240 |
if 100000000<=Number then begin |
| 3241 |
Power:=100000000; |
| 3242 |
Exponent:=8; |
| 3243 |
end else begin |
| 3244 |
goto c3; |
| 3245 |
end; |
| 3246 |
end; |
| 3247 |
24,25,26:begin |
| 3248 |
c3: |
| 3249 |
if 10000000<=Number then begin |
| 3250 |
Power:=10000000; |
| 3251 |
Exponent:=7; |
| 3252 |
end else begin |
| 3253 |
goto c4; |
| 3254 |
end; |
| 3255 |
end; |
| 3256 |
20,21,22,23:begin |
| 3257 |
c4: |
| 3258 |
if 1000000<=Number then begin |
| 3259 |
Power:=1000000; |
| 3260 |
Exponent:=6; |
| 3261 |
end else begin |
| 3262 |
goto c5; |
| 3263 |
end; |
| 3264 |
end; |
| 3265 |
17,18,19:begin |
| 3266 |
c5: |
| 3267 |
if 100000<=Number then begin |
| 3268 |
Power:=100000; |
| 3269 |
Exponent:=5; |
| 3270 |
end else begin |
| 3271 |
goto c6; |
| 3272 |
end; |
| 3273 |
end; |
| 3274 |
14,15,16:begin |
| 3275 |
c6: |
| 3276 |
if 10000<=Number then begin |
| 3277 |
Power:=10000; |
| 3278 |
Exponent:=4; |
| 3279 |
end else begin |
| 3280 |
goto c7; |
| 3281 |
end; |
| 3282 |
end; |
| 3283 |
10,11,12,13:begin |
| 3284 |
c7: |
| 3285 |
if 1000<=Number then begin |
| 3286 |
Power:=1000; |
| 3287 |
Exponent:=3; |
| 3288 |
end else begin |
| 3289 |
goto c8; |
| 3290 |
end; |
| 3291 |
end; |
| 3292 |
7,8,9:begin |
| 3293 |
c8: |
| 3294 |
if 100<=Number then begin |
| 3295 |
Power:=100; |
| 3296 |
Exponent:=2; |
| 3297 |
end else begin |
| 3298 |
goto c9; |
| 3299 |
end; |
| 3300 |
end; |
| 3301 |
4,5,6:begin |
| 3302 |
c9: |
| 3303 |
if 10<=Number then begin |
| 3304 |
Power:=10; |
| 3305 |
Exponent:=1; |
| 3306 |
end else begin |
| 3307 |
goto c10; |
| 3308 |
end; |
| 3309 |
end; |
| 3310 |
1,2,3:begin |
| 3311 |
c10: |
| 3312 |
if 1<=Number then begin |
| 3313 |
Power:=1; |
| 3314 |
Exponent:=0; |
| 3315 |
end else begin |
| 3316 |
goto c11; |
| 3317 |
end; |
| 3318 |
end; |
| 3319 |
0:begin |
| 3320 |
c11: |
| 3321 |
Power:=0; |
| 3322 |
Exponent:=-1; |
| 3323 |
end; |
| 3324 |
else begin |
| 3325 |
Power:=0; |
| 3326 |
Exponent:=0; |
| 3327 |
result:=false; |
| 3328 |
end; |
| 3329 |
end; |
| 3330 |
end; |
| 3331 |
function DigitGen(Low,w,High:TDoubleValue;var Buffer:TPasDblStrUtilsString;var Len,Capacity:TPasDblStrUtilsInt32):TPasDblStrUtilsBoolean; |
| 3332 |
var UnitValue,Fractionals,Rest:TPasDblStrUtilsUInt64; |
| 3333 |
TooLow,TooHigh,UnsafeInterval,One:TDoubleValue; |
| 3334 |
Integrals,Divisor,Digit:TPasDblStrUtilsUInt32; |
| 3335 |
DivisorExponent:TPasDblStrUtilsInt32; |
| 3336 |
begin |
| 3337 |
result:=false; |
| 3338 |
if ((Low.Exponent=w.Exponent) and (w.Exponent=High.Exponent)) and (QWordLessOrEqual(Low.SignificantMantissa+1,High.SignificantMantissa-1) and |
| 3339 |
((MinimalTargetExponent<=w.Exponent) and (w.Exponent<=MaximalTargetExponent))) then begin |
| 3340 |
UnitValue:=1; |
| 3341 |
TooLow.SignificantMantissa:=Low.SignificantMantissa-UnitValue; |
| 3342 |
TooLow.Exponent:=Low.Exponent; |
| 3343 |
TooHigh.SignificantMantissa:=High.SignificantMantissa+UnitValue; |
| 3344 |
TooHigh.Exponent:=High.Exponent; |
| 3345 |
UnsafeInterval:=DoubleValueMinus(TooHigh,TooLow); |
| 3346 |
One.SignificantMantissa:=TPasDblStrUtilsUInt64(1) shl (-w.Exponent); |
| 3347 |
One.Exponent:=w.Exponent; |
| 3348 |
Integrals:=TooHigh.SignificantMantissa shr (-One.Exponent); |
| 3349 |
Fractionals:=TooHigh.SignificantMantissa and (One.SignificantMantissa-1); |
| 3350 |
Divisor:=0; |
| 3351 |
DivisorExponent:=0; |
| 3352 |
if BiggestPowerTen(Integrals,SignificantMantissaSize-(-One.Exponent),Divisor,DivisorExponent) then begin |
| 3353 |
Capacity:=DivisorExponent+1; |
| 3354 |
Len:=0; |
| 3355 |
while Capacity>0 do begin |
| 3356 |
Digit:=Integrals div Divisor; |
| 3357 |
Integrals:=Integrals mod Divisor; |
| 3358 |
inc(Len); |
| 3359 |
if Len>=length(Buffer) then begin |
| 3360 |
SetLength(Buffer,Len*2); |
| 3361 |
end; |
| 3362 |
Buffer[Len]:=TPasDblStrUtilsChar(TPasDblStrUtilsUInt8(TPasDblStrUtilsUInt8(TPasDblStrUtilsChar('0'))+Digit)); |
| 3363 |
dec(Capacity); |
| 3364 |
Rest:=TPasDblStrUtilsUInt64(TPasDblStrUtilsUInt64(Integrals) shl (-One.Exponent))+Fractionals; |
| 3365 |
if QWordLess(Rest,UnsafeInterval.SignificantMantissa) then begin |
| 3366 |
result:=RoundWeed(Buffer,Len,DoubleValueMinus(TooHigh,w).SignificantMantissa,UnsafeInterval.SignificantMantissa,Rest,TPasDblStrUtilsUInt64(Divisor) shl (-One.Exponent),UnitValue); |
| 3367 |
exit; |
| 3368 |
end; |
| 3369 |
Divisor:=Divisor div 10; |
| 3370 |
end; |
| 3371 |
if (One.Exponent>=-60) and (QWordLess(Fractionals,One.SignificantMantissa) and QWordGreaterOrEqual(TPasDblStrUtilsUInt64($1999999999999999),One.SignificantMantissa)) then begin |
| 3372 |
while true do begin |
| 3373 |
Fractionals:=Fractionals*10; |
| 3374 |
UnitValue:=UnitValue*10; |
| 3375 |
UnsafeInterval.SignificantMantissa:=UnsafeInterval.SignificantMantissa*10; |
| 3376 |
Digit:=Fractionals shr (-One.Exponent); |
| 3377 |
inc(Len); |
| 3378 |
if Len>=length(Buffer) then begin |
| 3379 |
SetLength(Buffer,Len*2); |
| 3380 |
end; |
| 3381 |
Buffer[Len]:=TPasDblStrUtilsChar(TPasDblStrUtilsUInt8(TPasDblStrUtilsUInt8(TPasDblStrUtilsChar('0'))+Digit)); |
| 3382 |
dec(Capacity); |
| 3383 |
Fractionals:=Fractionals and (One.SignificantMantissa-1); |
| 3384 |
if QWordLess(Fractionals,UnsafeInterval.SignificantMantissa) then begin |
| 3385 |
result:=RoundWeed(Buffer,Len,DoubleValueMinus(TooHigh,w).SignificantMantissa*UnitValue,UnsafeInterval.SignificantMantissa,Fractionals,One.SignificantMantissa,UnitValue); |
| 3386 |
exit; |
| 3387 |
end; |
| 3388 |
end; |
| 3389 |
end; |
| 3390 |
end; |
| 3391 |
end; |
| 3392 |
end; |
| 3393 |
function DigitGenCounted(w:TDoubleValue;RequestedDigits:TPasDblStrUtilsInt32;var Buffer:TPasDblStrUtilsString;var Len,Capacity:TPasDblStrUtilsInt32):TPasDblStrUtilsBoolean; |
| 3394 |
var wError,Fractionals,Rest:TPasDblStrUtilsUInt64; |
| 3395 |
One:TDoubleValue; |
| 3396 |
Integrals,Divisor,Digit:TPasDblStrUtilsUInt32; |
| 3397 |
DivisorExponent:TPasDblStrUtilsInt32; |
| 3398 |
begin |
| 3399 |
result:=false; |
| 3400 |
if ((MinimalTargetExponent<=w.Exponent) and (w.Exponent<=MaximalTargetExponent)) and ((MinimalTargetExponent>=-60) and (MaximalTargetExponent<=-32)) then begin |
| 3401 |
wError:=1; |
| 3402 |
One.SignificantMantissa:=TPasDblStrUtilsUInt64(1) shl (-w.Exponent); |
| 3403 |
One.Exponent:=w.Exponent; |
| 3404 |
Integrals:=w.SignificantMantissa shr (-One.Exponent); |
| 3405 |
Fractionals:=w.SignificantMantissa and (One.SignificantMantissa-1); |
| 3406 |
Divisor:=0; |
| 3407 |
DivisorExponent:=0; |
| 3408 |
if BiggestPowerTen(Integrals,SignificantMantissaSize-(-One.Exponent),Divisor,DivisorExponent) then begin |
| 3409 |
Capacity:=DivisorExponent+1; |
| 3410 |
Len:=0; |
| 3411 |
while Capacity>0 do begin |
| 3412 |
Digit:=Integrals div Divisor; |
| 3413 |
Integrals:=Integrals mod Divisor; |
| 3414 |
inc(Len); |
| 3415 |
if Len>=length(Buffer) then begin |
| 3416 |
SetLength(Buffer,Len*2); |
| 3417 |
end; |
| 3418 |
Buffer[Len]:=TPasDblStrUtilsChar(TPasDblStrUtilsUInt8(TPasDblStrUtilsUInt8(TPasDblStrUtilsChar('0'))+Digit)); |
| 3419 |
dec(RequestedDigits); |
| 3420 |
dec(Capacity); |
| 3421 |
if RequestedDigits=0 then begin |
| 3422 |
break; |
| 3423 |
end; |
| 3424 |
Divisor:=Divisor div 10; |
| 3425 |
end; |
| 3426 |
if RequestedDigits=0 then begin |
| 3427 |
Rest:=TPasDblStrUtilsUInt64(TPasDblStrUtilsUInt64(Integrals) shl (-One.Exponent))+Fractionals; |
| 3428 |
result:=RoundWeedCounted(Buffer,Len,Rest,TPasDblStrUtilsUInt64(Divisor) shl (-One.Exponent),wError,Capacity); |
| 3429 |
exit; |
| 3430 |
end; |
| 3431 |
if ((One.Exponent>=-60) and QWordLess(Fractionals,One.SignificantMantissa)) and QWordGreaterOrEqual(TPasDblStrUtilsUInt64($1999999999999999),One.SignificantMantissa) then begin |
| 3432 |
while (RequestedDigits>0) and (Fractionals>wError) do begin |
| 3433 |
Fractionals:=Fractionals*10; |
| 3434 |
Digit:=Fractionals shr (-One.Exponent); |
| 3435 |
inc(Len); |
| 3436 |
if Len>=length(Buffer) then begin |
| 3437 |
SetLength(Buffer,Len*2); |
| 3438 |
end; |
| 3439 |
Buffer[Len]:=TPasDblStrUtilsChar(TPasDblStrUtilsUInt8(TPasDblStrUtilsUInt8(TPasDblStrUtilsChar('0'))+Digit)); |
| 3440 |
dec(RequestedDigits); |
| 3441 |
dec(Capacity); |
| 3442 |
Fractionals:=Fractionals and (One.SignificantMantissa-1); |
| 3443 |
end; |
| 3444 |
if RequestedDigits=0 then begin |
| 3445 |
result:=RoundWeedCounted(Buffer,Len,Fractionals,One.SignificantMantissa,wError,Capacity); |
| 3446 |
end else begin |
| 3447 |
result:=false; |
| 3448 |
end; |
| 3449 |
end; |
| 3450 |
end; |
| 3451 |
end; |
| 3452 |
end; |
| 3453 |
procedure NormalizedBoundaries(Value:TPasDblStrUtilsDouble;var BoundaryMinus,BoundaryPlus:TDoubleValue); |
| 3454 |
var v:TDoubleValue; |
| 3455 |
SignificantMantissaIsZero:TPasDblStrUtilsBoolean; |
| 3456 |
begin |
| 3457 |
Assert(not IsNegative(Value)); |
| 3458 |
Assert(IsFinite(Value)); |
| 3459 |
SplitDouble(Value,v.SignificantMantissa,v.Exponent); |
| 3460 |
SignificantMantissaIsZero:=v.SignificantMantissa=TPasDblStrUtilsUInt64($0010000000000000); |
| 3461 |
BoundaryPlus.SignificantMantissa:=(v.SignificantMantissa shl 1)+1; |
| 3462 |
BoundaryPlus.Exponent:=v.Exponent-1; |
| 3463 |
DoubleValueNormalize(BoundaryPlus); |
| 3464 |
if SignificantMantissaIsZero and (v.Exponent<>((-($3ff+52))+1)) then begin |
| 3465 |
BoundaryMinus.SignificantMantissa:=(v.SignificantMantissa shl 2)-1; |
| 3466 |
BoundaryMinus.Exponent:=v.Exponent-2; |
| 3467 |
end else begin |
| 3468 |
BoundaryMinus.SignificantMantissa:=(v.SignificantMantissa shl 1)-1; |
| 3469 |
BoundaryMinus.Exponent:=v.Exponent-1; |
| 3470 |
end; |
| 3471 |
BoundaryMinus.SignificantMantissa:=BoundaryMinus.SignificantMantissa shl (BoundaryMinus.Exponent-BoundaryPlus.Exponent); |
| 3472 |
BoundaryMinus.Exponent:=BoundaryPlus.Exponent; |
| 3473 |
end; |
| 3474 |
function DoFastShortest(Value:TPasDblStrUtilsDouble;var Buffer:TPasDblStrUtilsString;var Len,DecimalExponent:TPasDblStrUtilsInt32):TPasDblStrUtilsBoolean; |
| 3475 |
var w,BoundaryMinus,BoundaryPlus,TenMK,ScaledW,ScaledBoundaryMinus,ScaledBoundaryPlus:TDoubleValue; |
| 3476 |
mK,TenMKMinimalBinaryExponent,TenMKMaximalBinaryExponent,Capacity:TPasDblStrUtilsInt32; |
| 3477 |
begin |
| 3478 |
result:=false; |
| 3479 |
w:=DoubleValueGet(Value); |
| 3480 |
NormalizedBoundaries(Value,BoundaryMinus,BoundaryPlus); |
| 3481 |
Assert(BoundaryPlus.Exponent=w.Exponent); |
| 3482 |
TenMKMinimalBinaryExponent:=MinimalTargetExponent-(w.Exponent+SignificantMantissaSize); |
| 3483 |
TenMKMaximalBinaryExponent:=MaximalTargetExponent-(w.Exponent+SignificantMantissaSize); |
| 3484 |
if GetCachedPowerForBinaryExponentRange(TenMKMinimalBinaryExponent,TenMKMaximalBinaryExponent,TenMK,mK) then begin |
| 3485 |
if (MinimalTargetExponent<=(w.Exponent+TenMK.Exponent+SignificantMantissaSize)) and (MaximalTargetExponent>=(w.Exponent+TenMK.Exponent+SignificantMantissaSize)) then begin |
| 3486 |
ScaledW:=DoubleValueMul(w,TenMK); |
| 3487 |
if ScaledW.Exponent=(BoundaryPlus.Exponent+TenMK.Exponent+SignificantMantissaSize) then begin |
| 3488 |
ScaledBoundaryMinus:=DoubleValueMul(BoundaryMinus,TenMK); |
| 3489 |
ScaledBoundaryPlus:=DoubleValueMul(BoundaryPlus,TenMK); |
| 3490 |
Capacity:=0; |
| 3491 |
result:=DigitGen(ScaledBoundaryMinus,ScaledW,ScaledBoundaryPlus,Buffer,Len,Capacity); |
| 3492 |
DecimalExponent:=Capacity-mK; |
| 3493 |
end; |
| 3494 |
end; |
| 3495 |
end; |
| 3496 |
end; |
| 3497 |
function DoFastPrecision(Value:TPasDblStrUtilsDouble;RequestedDigits:TPasDblStrUtilsInt32;var Buffer:TPasDblStrUtilsString;var Len,DecimalExponent:TPasDblStrUtilsInt32):TPasDblStrUtilsBoolean; |
| 3498 |
var w,TenMK,ScaledW:TDoubleValue; |
| 3499 |
mK,TenMKMinimalBinaryExponent,TenMKMaximalBinaryExponent,Capacity:TPasDblStrUtilsInt32; |
| 3500 |
begin |
| 3501 |
result:=false; |
| 3502 |
w:=DoubleValueGet(Value); |
| 3503 |
TenMKMinimalBinaryExponent:=MinimalTargetExponent-(w.Exponent+SignificantMantissaSize); |
| 3504 |
TenMKMaximalBinaryExponent:=MaximalTargetExponent-(w.Exponent+SignificantMantissaSize); |
| 3505 |
if GetCachedPowerForBinaryExponentRange(TenMKMinimalBinaryExponent,TenMKMaximalBinaryExponent,TenMK,mK) then begin |
| 3506 |
if (MinimalTargetExponent<=(w.Exponent+TenMK.Exponent+SignificantMantissaSize)) and (MaximalTargetExponent>=(w.Exponent+TenMK.Exponent+SignificantMantissaSize)) then begin |
| 3507 |
ScaledW:=DoubleValueMul(w,TenMK); |
| 3508 |
Capacity:=0; |
| 3509 |
result:=DigitGenCounted(ScaledW,RequestedDigits,Buffer,Len,Capacity); |
| 3510 |
DecimalExponent:=Capacity-mK; |
| 3511 |
end; |
| 3512 |
end; |
| 3513 |
end; |
| 3514 |
function DoFastFixed(Value:TPasDblStrUtilsDouble;FracitionalCount:TPasDblStrUtilsInt32;var Buffer:TPasDblStrUtilsString;var Len,DecimalPoint:TPasDblStrUtilsInt32):TPasDblStrUtilsBoolean; |
| 3515 |
const Five17=$b1a2bc2ec5; // 5^17 |
| 3516 |
type TInt128=record |
| 3517 |
High,Low:TPasDblStrUtilsUInt64; |
| 3518 |
end; |
| 3519 |
procedure Int128Mul(var a:TInt128;const Multiplicand:TPasDblStrUtilsUInt32); |
| 3520 |
var Accumulator:TPasDblStrUtilsUInt64; |
| 3521 |
Part:TPasDblStrUtilsUInt32; |
| 3522 |
begin |
| 3523 |
Accumulator:=(a.Low and $ffffffff)*Multiplicand; |
| 3524 |
Part:=Accumulator and $ffffffff; |
| 3525 |
Accumulator:=(Accumulator shr 32)+((a.Low shr 32)*Multiplicand); |
| 3526 |
a.Low:=(Accumulator shl 32)+Part; |
| 3527 |
Accumulator:=(Accumulator shr 32)+((a.High and $ffffffff)*Multiplicand); |
| 3528 |
Part:=Accumulator and $ffffffff; |
| 3529 |
Accumulator:=(Accumulator shr 32)+((a.High shr 32)*Multiplicand); |
| 3530 |
a.High:=(Accumulator shl 32)+Part; |
| 3531 |
Assert((Accumulator shr 32)=0); |
| 3532 |
end; |
| 3533 |
procedure Int128Shift(var a:TInt128;const Shift:TPasDblStrUtilsInt32); |
| 3534 |
begin |
| 3535 |
Assert(((-64)<=Shift) and (Shift<=64)); |
| 3536 |
if Shift<>0 then begin |
| 3537 |
if Shift=-64 then begin |
| 3538 |
a.High:=a.Low; |
| 3539 |
a.Low:=0; |
| 3540 |
end else if Shift=64 then begin |
| 3541 |
a.Low:=a.High; |
| 3542 |
a.High:=0; |
| 3543 |
end else if Shift<=0 then begin |
| 3544 |
a.High:=(a.High shl (-Shift))+(a.Low shr (64+Shift)); |
| 3545 |
a.Low:=a.Low shl (-Shift); |
| 3546 |
end else begin |
| 3547 |
a.Low:=(a.Low shr Shift)+(a.High shl (64-Shift)); |
| 3548 |
a.High:=a.High shr Shift; |
| 3549 |
end; |
| 3550 |
end; |
| 3551 |
end; |
| 3552 |
function Int128DivModPowerOfTwo(var a:TInt128;const Power:TPasDblStrUtilsInt32):TPasDblStrUtilsInt32; |
| 3553 |
begin |
| 3554 |
if Power>=64 then begin |
| 3555 |
result:=a.High shr (Power-64); |
| 3556 |
dec(a.High,result shl (Power-64)); |
| 3557 |
end else begin |
| 3558 |
result:=(a.Low shr Power)+(a.High shl (64-Power)); |
| 3559 |
a.High:=0; |
| 3560 |
dec(a.Low,(a.Low shr Power) shl Power); |
| 3561 |
end; |
| 3562 |
end; |
| 3563 |
function Int128IsZero(const a:TInt128):TPasDblStrUtilsBoolean; |
| 3564 |
begin |
| 3565 |
result:=(a.High=0) and (a.Low=0); |
| 3566 |
end; |
| 3567 |
function Int128BitAt(const a:TInt128;const Position:TPasDblStrUtilsInt32):TPasDblStrUtilsBoolean; |
| 3568 |
begin |
| 3569 |
if Position>=64 then begin |
| 3570 |
result:=((a.High shr (Position-64)) and 1)<>0; |
| 3571 |
end else begin |
| 3572 |
result:=((a.LOw shr Position) and 1)<>0; |
| 3573 |
end; |
| 3574 |
end; |
| 3575 |
procedure FillDigits32FixedLength(Number:TPasDblStrUtilsUInt32;RequestedLength:TPasDblStrUtilsInt32;var Buffer:TPasDblStrUtilsString;var Len:TPasDblStrUtilsInt32); |
| 3576 |
var i,l:TPasDblStrUtilsInt32; |
| 3577 |
begin |
| 3578 |
l:=Len; |
| 3579 |
inc(Len,RequestedLength); |
| 3580 |
if Len>=length(Buffer) then begin |
| 3581 |
SetLength(Buffer,Len*2); |
| 3582 |
end; |
| 3583 |
for i:=RequestedLength downto 1 do begin |
| 3584 |
Buffer[l+i]:=TPasDblStrUtilsChar(TPasDblStrUtilsUInt8(TPasDblStrUtilsUInt8(TPasDblStrUtilsChar('0'))+(Number mod 10))); |
| 3585 |
Number:=Number div 10; |
| 3586 |
end; |
| 3587 |
end; |
| 3588 |
procedure FillDigits32(Number:TPasDblStrUtilsUInt32;var Buffer:TPasDblStrUtilsString;var Len:TPasDblStrUtilsInt32); |
| 3589 |
var NumberLength,i,l:TPasDblStrUtilsInt32; |
| 3590 |
OldNumber:TPasDblStrUtilsUInt32; |
| 3591 |
begin |
| 3592 |
OldNumber:=Number; |
| 3593 |
NumberLength:=0; |
| 3594 |
while Number<>0 do begin |
| 3595 |
Number:=Number div 10; |
| 3596 |
inc(NumberLength); |
| 3597 |
end; |
| 3598 |
if NumberLength<>0 then begin |
| 3599 |
l:=Len; |
| 3600 |
inc(Len,NumberLength); |
| 3601 |
if Len>=length(Buffer) then begin |
| 3602 |
SetLength(Buffer,Len*2); |
| 3603 |
end; |
| 3604 |
Number:=OldNumber; |
| 3605 |
for i:=NumberLength downto 1 do begin |
| 3606 |
Buffer[l+i]:=TPasDblStrUtilsChar(TPasDblStrUtilsUInt8(TPasDblStrUtilsUInt8(TPasDblStrUtilsChar('0'))+(Number mod 10))); |
| 3607 |
Number:=Number div 10; |
| 3608 |
end; |
| 3609 |
end; |
| 3610 |
end; |
| 3611 |
procedure FillDigits64FixedLength(Number:TPasDblStrUtilsUInt64;RequestedLength:TPasDblStrUtilsInt32;var Buffer:TPasDblStrUtilsString;var Len:TPasDblStrUtilsInt32); |
| 3612 |
var p0,p1,p2:TPasDblStrUtilsUInt32; |
| 3613 |
begin |
| 3614 |
p2:=Number mod 10000000; |
| 3615 |
Number:=Number div 10000000; |
| 3616 |
p1:=Number mod 10000000; |
| 3617 |
p0:=Number div 10000000; |
| 3618 |
FillDigits32FixedLength(p0,3,Buffer,Len); |
| 3619 |
FillDigits32FixedLength(p1,7,Buffer,Len); |
| 3620 |
FillDigits32FixedLength(p2,7,Buffer,Len); |
| 3621 |
end; |
| 3622 |
procedure FillDigits64(Number:TPasDblStrUtilsUInt64;var Buffer:TPasDblStrUtilsString;var Len:TPasDblStrUtilsInt32); |
| 3623 |
var p0,p1,p2:TPasDblStrUtilsUInt32; |
| 3624 |
begin |
| 3625 |
p2:=Number mod 10000000; |
| 3626 |
Number:=Number div 10000000; |
| 3627 |
p1:=Number mod 10000000; |
| 3628 |
p0:=Number div 10000000; |
| 3629 |
if p0<>0 then begin |
| 3630 |
FillDigits32(p0,Buffer,Len); |
| 3631 |
FillDigits32FixedLength(p1,7,Buffer,Len); |
| 3632 |
FillDigits32FixedLength(p2,7,Buffer,Len); |
| 3633 |
end else if p1<>0 then begin |
| 3634 |
FillDigits32(p1,Buffer,Len); |
| 3635 |
FillDigits32FixedLength(p2,7,Buffer,Len); |
| 3636 |
end else begin |
| 3637 |
FillDigits32(p2,Buffer,Len); |
| 3638 |
end; |
| 3639 |
end; |
| 3640 |
procedure RoundUp(var Buffer:TPasDblStrUtilsString;var Len,DecimalPoint:TPasDblStrUtilsInt32); |
| 3641 |
var i:TPasDblStrUtilsInt32; |
| 3642 |
begin |
| 3643 |
if Len=0 then begin |
| 3644 |
Buffer:='1'; |
| 3645 |
Len:=1; |
| 3646 |
DecimalPoint:=1; |
| 3647 |
end else begin |
| 3648 |
inc(Buffer[Len]); |
| 3649 |
for i:=Len downto 2 do begin |
| 3650 |
if ord(Buffer[i])<>(ord('0')+10) then begin |
| 3651 |
exit; |
| 3652 |
end; |
| 3653 |
Buffer[i]:='0'; |
| 3654 |
inc(Buffer[i-1]); |
| 3655 |
end; |
| 3656 |
if ord(Buffer[1])=(ord('0')+10) then begin |
| 3657 |
Buffer[1]:='1'; |
| 3658 |
inc(DecimalPoint); |
| 3659 |
end; |
| 3660 |
end; |
| 3661 |
end; |
| 3662 |
procedure FillFractionals(Fractionals:TPasDblStrUtilsUInt64;Exponent:TPasDblStrUtilsInt32;FractionalCount:TPasDblStrUtilsInt32;var Buffer:TPasDblStrUtilsString;var Len,DecimalPoint:TPasDblStrUtilsInt32); |
| 3663 |
var Point,i,Digit:TPasDblStrUtilsInt32; |
| 3664 |
Fractionals128:TInt128; |
| 3665 |
begin |
| 3666 |
Assert(((-128)<=Exponent) and (Exponent<=0)); |
| 3667 |
if (-Exponent)<=64 then begin |
| 3668 |
Assert((Fractionals shr 56)=0); |
| 3669 |
Point:=-Exponent; |
| 3670 |
for i:=1 to FracitionalCount do begin |
| 3671 |
Fractionals:=Fractionals*5; |
| 3672 |
dec(Point); |
| 3673 |
Digit:=Fractionals shr Point; |
| 3674 |
inc(Len); |
| 3675 |
if Len>=length(Buffer) then begin |
| 3676 |
SetLength(Buffer,Len*2); |
| 3677 |
end; |
| 3678 |
Buffer[Len]:=TPasDblStrUtilsChar(TPasDblStrUtilsUInt8(TPasDblStrUtilsUInt8(TPasDblStrUtilsChar('0'))+Digit)); |
| 3679 |
dec(Fractionals,TPasDblStrUtilsUInt64(Digit) shl Point); |
| 3680 |
end; |
| 3681 |
if ((Fractionals shr (Point-1)) and 1)<>0 then begin |
| 3682 |
RoundUp(Buffer,Len,DecimalPoint); |
| 3683 |
end; |
| 3684 |
end else begin |
| 3685 |
Assert((64<(-Exponent)) and ((-Exponent)<=128)); |
| 3686 |
Fractionals128.High:=Fractionals; |
| 3687 |
Fractionals128.Low:=0; |
| 3688 |
Int128Shift(Fractionals128,(-Exponent)-64); |
| 3689 |
Point:=128; |
| 3690 |
for i:=1 to FracitionalCount do begin |
| 3691 |
if Int128IsZero(Fractionals128) then begin |
| 3692 |
break; |
| 3693 |
end; |
| 3694 |
Int128Mul(Fractionals128,5); |
| 3695 |
dec(Point); |
| 3696 |
Digit:=Int128DivModPowerOfTwo(Fractionals128,Point); |
| 3697 |
inc(Len); |
| 3698 |
if Len>=length(Buffer) then begin |
| 3699 |
SetLength(Buffer,Len*2); |
| 3700 |
end; |
| 3701 |
Buffer[Len]:=TPasDblStrUtilsChar(TPasDblStrUtilsUInt8(TPasDblStrUtilsUInt8(TPasDblStrUtilsChar('0'))+Digit)); |
| 3702 |
end; |
| 3703 |
if Int128BitAt(Fractionals128,Point-1) then begin |
| 3704 |
RoundUp(Buffer,Len,DecimalPoint); |
| 3705 |
end; |
| 3706 |
end; |
| 3707 |
end; |
| 3708 |
procedure TrimZeros(var Buffer:TPasDblStrUtilsString;var Len,DecimalPoint:TPasDblStrUtilsInt32); |
| 3709 |
var i:TPasDblStrUtilsInt32; |
| 3710 |
begin |
| 3711 |
while (Len>0) and (Buffer[Len]='0') do begin |
| 3712 |
dec(Len); |
| 3713 |
end; |
| 3714 |
i:=0; |
| 3715 |
while (i<Len) and (Buffer[i+1]='0') do begin |
| 3716 |
inc(i); |
| 3717 |
end; |
| 3718 |
if i<>0 then begin |
| 3719 |
Delete(Buffer,1,i); |
| 3720 |
dec(Len,i); |
| 3721 |
dec(DecimalPoint,i); |
| 3722 |
end; |
| 3723 |
end; |
| 3724 |
var SignificantMantissa,Divisor,Dividend,Remainder,Integrals,Fractionals:TPasDblStrUtilsUInt64; |
| 3725 |
Exponent,DivisorPower:TPasDblStrUtilsInt32; |
| 3726 |
Quotient:TPasDblStrUtilsUInt32; |
| 3727 |
begin |
| 3728 |
result:=false; |
| 3729 |
SplitDouble(Value,SignificantMantissa,Exponent); |
| 3730 |
if (Exponent<=20) and (FracitionalCount<=20) then begin |
| 3731 |
Len:=0; |
| 3732 |
if (Exponent+53)>74 then begin |
| 3733 |
Divisor:=Five17; |
| 3734 |
DivisorPower:=17; |
| 3735 |
Dividend:=SignificantMantissa; |
| 3736 |
if Exponent>DivisorPower then begin |
| 3737 |
Dividend:=Dividend shl (Exponent-DivisorPower); |
| 3738 |
Quotient:=Dividend div Divisor; |
| 3739 |
Remainder:=(Dividend mod Divisor) shl DivisorPower; |
| 3740 |
end else begin |
| 3741 |
Dividend:=Dividend shl (DivisorPower-Exponent); |
| 3742 |
Quotient:=Dividend div Divisor; |
| 3743 |
Remainder:=(Dividend mod Divisor) shl Exponent; |
| 3744 |
end; |
| 3745 |
FillDigits32(Quotient,Buffer,Len); |
| 3746 |
FillDigits64FixedLength(Remainder,DivisorPower,Buffer,Len); |
| 3747 |
DecimalPoint:=Len; |
| 3748 |
end else if Exponent>=0 then begin |
| 3749 |
SignificantMantissa:=SignificantMantissa shl Exponent; |
| 3750 |
FillDigits64(SignificantMantissa,Buffer,Len); |
| 3751 |
DecimalPoint:=Len; |
| 3752 |
end else if Exponent>-53 then begin |
| 3753 |
Integrals:=SignificantMantissa shr (-Exponent); |
| 3754 |
Fractionals:=SignificantMantissa-(Integrals shl (-Exponent)); |
| 3755 |
if Integrals>$ffffffff then begin |
| 3756 |
FillDigits64(Integrals,Buffer,Len); |
| 3757 |
end else begin |
| 3758 |
FillDigits32(Integrals,Buffer,Len); |
| 3759 |
end; |
| 3760 |
DecimalPoint:=Len; |
| 3761 |
FillFractionals(Fractionals,Exponent,FracitionalCount,Buffer,Len,DecimalPoint); |
| 3762 |
end else if Exponent<-128 then begin |
| 3763 |
Assert(FracitionalCount>=20); |
| 3764 |
Buffer:=''; |
| 3765 |
Len:=0; |
| 3766 |
DecimalPoint:=-FracitionalCount; |
| 3767 |
end else begin |
| 3768 |
DecimalPoint:=0; |
| 3769 |
FillFractionals(SignificantMantissa,Exponent,FracitionalCount,Buffer,Len,DecimalPoint); |
| 3770 |
end; |
| 3771 |
TrimZeros(Buffer,Len,DecimalPoint); |
| 3772 |
SetLength(Buffer,Len); |
| 3773 |
if Len=0 then begin |
| 3774 |
DecimalPoint:=-FracitionalCount; |
| 3775 |
end; |
| 3776 |
result:=true; |
| 3777 |
end; |
| 3778 |
end; |
| 3779 |
var OK,Fast:TPasDblStrUtilsBoolean; |
| 3780 |
Len,DecimalPoint,ZeroPrefixLength,ZeroPostfixLength,i:TPasDblStrUtilsInt32; |
| 3781 |
LocalOutputMode:TPasDblStrUtilsOutputMode; |
| 3782 |
begin |
| 3783 |
if IsNaN(AValue) then begin |
| 3784 |
result:='NaN'; |
| 3785 |
end else if IsZero(AValue) then begin |
| 3786 |
result:='0'; |
| 3787 |
end else if IsNegInfinite(AValue) then begin |
| 3788 |
result:='-Infinity'; |
| 3789 |
end else if IsNegative(AValue) then begin |
| 3790 |
result:='-'+ConvertDoubleToString(DoubleAbsolute(AValue),OutputMode,RequestedDigits); |
| 3791 |
end else if IsInfinite(AValue) then begin |
| 3792 |
result:='Infinity'; |
| 3793 |
end else begin |
| 3794 |
result:='0'; |
| 3795 |
if AValue<>0 then begin |
| 3796 |
Len:=0; |
| 3797 |
DecimalPoint:=0; |
| 3798 |
OK:=false; |
| 3799 |
Fast:=false; |
| 3800 |
if ((OutputMode=omFixed) and (AValue>=1e21)) or ((OutputMode=omRadix) and (RequestedDigits=10)) then begin |
| 3801 |
LocalOutputMode:=omStandard; |
| 3802 |
end else begin |
| 3803 |
LocalOutputMode:=OutputMode; |
| 3804 |
end; |
| 3805 |
case LocalOutputMode of |
| 3806 |
omStandard,omStandardExponential:begin |
| 3807 |
OK:=DoFastShortest(AValue,result,Len,DecimalPoint); |
| 3808 |
inc(DecimalPoint,Len); |
| 3809 |
end; |
| 3810 |
omFixed:begin |
| 3811 |
OK:=DoFastFixed(AValue,RequestedDigits,result,Len,DecimalPoint); |
| 3812 |
end; |
| 3813 |
omExponential,omPrecision:begin |
| 3814 |
if RequestedDigits<=0 then begin |
| 3815 |
OK:=DoFastShortest(AValue,result,Len,DecimalPoint); |
| 3816 |
inc(DecimalPoint,Len); |
| 3817 |
RequestedDigits:=Len-1; |
| 3818 |
end else begin |
| 3819 |
OK:=DoFastPrecision(AValue,RequestedDigits,result,Len,DecimalPoint); |
| 3820 |
inc(DecimalPoint,Len); |
| 3821 |
end; |
| 3822 |
Assert((Len>0) and (Len<=(RequestedDigits+1))); |
| 3823 |
end; |
| 3824 |
omRadix:begin |
| 3825 |
if ((RequestedDigits>=2) and (RequestedDigits<=36)) and (IsFinite(AValue) and (AValue<4294967295.0) and (System.Int(AValue)=AValue)) then begin |
| 3826 |
FastDoubleToRadix(AValue,RequestedDigits,result,Len,DecimalPoint); |
| 3827 |
Fast:=true; |
| 3828 |
OK:=true; |
| 3829 |
end; |
| 3830 |
end; |
| 3831 |
end; |
| 3832 |
if not OK then begin |
| 3833 |
case LocalOutputMode of |
| 3834 |
omStandard,omStandardExponential:begin |
| 3835 |
DoubleToDecimal(AValue,ModeShortest,RequestedDigits,result,Len,DecimalPoint); |
| 3836 |
OK:=true; |
| 3837 |
end; |
| 3838 |
omFixed:begin |
| 3839 |
DoubleToDecimal(AValue,ModeFixed,RequestedDigits,result,Len,DecimalPoint); |
| 3840 |
OK:=true; |
| 3841 |
end; |
| 3842 |
omExponential,omPrecision:begin |
| 3843 |
if RequestedDigits<=0 then begin |
| 3844 |
DoubleToDecimal(AValue,ModeShortest,RequestedDigits,result,Len,DecimalPoint); |
| 3845 |
OK:=true; |
| 3846 |
RequestedDigits:=Len-1; |
| 3847 |
end else begin |
| 3848 |
DoubleToDecimal(AValue,ModePrecision,RequestedDigits,result,Len,DecimalPoint); |
| 3849 |
OK:=true; |
| 3850 |
end; |
| 3851 |
Assert((Len>0) and (Len<=(RequestedDigits+1))); |
| 3852 |
end; |
| 3853 |
omRadix:begin |
| 3854 |
if (RequestedDigits>=2) and (RequestedDigits<=36) then begin |
| 3855 |
DoubleToRadix(AValue,RequestedDigits,result,Len,DecimalPoint); |
| 3856 |
OK:=true; |
| 3857 |
end; |
| 3858 |
end; |
| 3859 |
end; |
| 3860 |
end; |
| 3861 |
if OK then begin |
| 3862 |
SetLength(result,Len); |
| 3863 |
case LocalOutputMode of |
| 3864 |
omStandard:begin |
| 3865 |
if (Len<=DecimalPoint) and (DecimalPoint<=21) then begin |
| 3866 |
SetLength(result,DecimalPoint); |
| 3867 |
FillChar(result[Len+1],DecimalPoint-Len,'0'); |
| 3868 |
end else if (0<DecimalPoint) and (DecimalPoint<=21) then begin |
| 3869 |
Insert('.',result,DecimalPoint+1); |
| 3870 |
end else if (DecimalPoint<=0) and (DecimalPoint>-6) then begin |
| 3871 |
for i:=1 to -DecimalPoint do begin |
| 3872 |
result:='0'+result; |
| 3873 |
end; |
| 3874 |
result:='0.'+result; |
| 3875 |
end else begin |
| 3876 |
if Len<>1 then begin |
| 3877 |
Insert('.',result,2); |
| 3878 |
end; |
| 3879 |
if DecimalPoint>=0 then begin |
| 3880 |
result:=result+'e+'+TPasDblStrUtilsString(IntToStr(abs(DecimalPoint-1))); |
| 3881 |
end else begin |
| 3882 |
result:=result+'e-'+TPasDblStrUtilsString(IntToStr(abs(DecimalPoint-1))); |
| 3883 |
end; |
| 3884 |
end; |
| 3885 |
end; |
| 3886 |
omStandardExponential:begin |
| 3887 |
if Len<>1 then begin |
| 3888 |
Insert('.',result,2); |
| 3889 |
end; |
| 3890 |
if DecimalPoint>=0 then begin |
| 3891 |
result:=result+'e+'+TPasDblStrUtilsString(IntToStr(abs(DecimalPoint-1))); |
| 3892 |
end else begin |
| 3893 |
result:=result+'e-'+TPasDblStrUtilsString(IntToStr(abs(DecimalPoint-1))); |
| 3894 |
end; |
| 3895 |
end; |
| 3896 |
omFixed:begin |
| 3897 |
ZeroPrefixLength:=0; |
| 3898 |
ZeroPostfixLength:=0; |
| 3899 |
if DecimalPoint<=0 then begin |
| 3900 |
ZeroPrefixLength:=(-DecimalPoint)+1; |
| 3901 |
DecimalPoint:=1; |
| 3902 |
end; |
| 3903 |
if (ZeroPrefixLength+Len)<(DecimalPoint+RequestedDigits) then begin |
| 3904 |
ZeroPostfixLength:=((DecimalPoint+RequestedDigits)-Len)-ZeroPrefixLength; |
| 3905 |
end; |
| 3906 |
for i:=1 to ZeroPrefixLength do begin |
| 3907 |
result:='0'+result; |
| 3908 |
end; |
| 3909 |
for i:=1 to ZeroPostfixLength do begin |
| 3910 |
result:=result+'0'; |
| 3911 |
end; |
| 3912 |
if (RequestedDigits>0) and (DecimalPoint>0) and (DecimalPoint<=length(result)) then begin |
| 3913 |
Insert('.',result,DecimalPoint+1); |
| 3914 |
end; |
| 3915 |
end; |
| 3916 |
omExponential:begin |
| 3917 |
if RequestedDigits<1 then begin |
| 3918 |
RequestedDigits:=1; |
| 3919 |
end; |
| 3920 |
if RequestedDigits<>1 then begin |
| 3921 |
Insert('.',result,2); |
| 3922 |
for i:=Len+1 to RequestedDigits do begin |
| 3923 |
result:=result+'0'; |
| 3924 |
end; |
| 3925 |
end else begin |
| 3926 |
SetLength(result,1); |
| 3927 |
end; |
| 3928 |
if DecimalPoint>=0 then begin |
| 3929 |
result:=result+'e+'+TPasDblStrUtilsString(IntToStr(abs(DecimalPoint-1))); |
| 3930 |
end else begin |
| 3931 |
result:=result+'e-'+TPasDblStrUtilsString(IntToStr(abs(DecimalPoint-1))); |
| 3932 |
end; |
| 3933 |
end; |
| 3934 |
omPrecision:begin |
| 3935 |
if RequestedDigits<1 then begin |
| 3936 |
RequestedDigits:=1; |
| 3937 |
end; |
| 3938 |
if (DecimalPoint<-6) or (DecimalPoint>=RequestedDigits) then begin |
| 3939 |
if RequestedDigits<>1 then begin |
| 3940 |
Insert('.',result,2); |
| 3941 |
for i:=Len+1 to RequestedDigits do begin |
| 3942 |
result:=result+'0'; |
| 3943 |
end; |
| 3944 |
end else begin |
| 3945 |
SetLength(result,1); |
| 3946 |
end; |
| 3947 |
if DecimalPoint>=0 then begin |
| 3948 |
result:=result+'e+'+TPasDblStrUtilsString(IntToStr(abs(DecimalPoint-1))); |
| 3949 |
end else begin |
| 3950 |
result:=result+'e-'+TPasDblStrUtilsString(IntToStr(abs(DecimalPoint-1))); |
| 3951 |
end; |
| 3952 |
end else begin |
| 3953 |
if DecimalPoint<=0 then begin |
| 3954 |
for i:=1 to -DecimalPoint do begin |
| 3955 |
result:='0'+result; |
| 3956 |
end; |
| 3957 |
result:='0.'+result; |
| 3958 |
for i:=Len+1 to RequestedDigits do begin |
| 3959 |
result:=result+'0'; |
| 3960 |
end; |
| 3961 |
end else begin |
| 3962 |
SetLength(result,RequestedDigits); |
| 3963 |
for i:=Len+1 to RequestedDigits do begin |
| 3964 |
result[i]:='0'; |
| 3965 |
end; |
| 3966 |
if DecimalPoint<RequestedDigits then begin |
| 3967 |
if Len<>1 then begin |
| 3968 |
Insert('.',result,DecimalPoint+1); |
| 3969 |
end; |
| 3970 |
end; |
| 3971 |
end; |
| 3972 |
end; |
| 3973 |
end; |
| 3974 |
omRadix:begin |
| 3975 |
if not Fast then begin |
| 3976 |
if (Len<=DecimalPoint) and (DecimalPoint<=21) then begin |
| 3977 |
SetLength(result,DecimalPoint); |
| 3978 |
FillChar(result[Len+1],DecimalPoint-Len,'0'); |
| 3979 |
end else if (0<DecimalPoint) and (DecimalPoint<=21) then begin |
| 3980 |
Insert('.',result,DecimalPoint+1); |
| 3981 |
end else if (DecimalPoint<=0) and (DecimalPoint>-6) then begin |
| 3982 |
for i:=1 to -DecimalPoint do begin |
| 3983 |
result:='0'+result; |
| 3984 |
end; |
| 3985 |
result:='0.'+result; |
| 3986 |
end else begin |
| 3987 |
if Len<>1 then begin |
| 3988 |
Insert('.',result,2); |
| 3989 |
end; |
| 3990 |
if DecimalPoint>=0 then begin |
| 3991 |
result:=result+'p+'+TPasDblStrUtilsString(IntToStr(abs(DecimalPoint-1))); |
| 3992 |
end else begin |
| 3993 |
result:=result+'p-'+TPasDblStrUtilsString(IntToStr(abs(DecimalPoint-1))); |
| 3994 |
end; |
| 3995 |
end; |
| 3996 |
while (length(result)>1) and ((result[1]='0') and (result[2] in ['0'..'9','a'..'f'])) do begin |
| 3997 |
Delete(result,1,1); |
| 3998 |
end; |
| 3999 |
end; |
| 4000 |
end; |
| 4001 |
end; |
| 4002 |
end else begin |
| 4003 |
result:=''; |
| 4004 |
end; |
| 4005 |
end; |
| 4006 |
end; |
| 4007 |
end; |
| 4008 |
|
| 4009 |
{$endif} |
| 4010 |
|
| 4011 |
initialization |
| 4012 |
finalization |
| 4013 |
end. |