OMCompiler/SimulationRuntime/c/meta/realString.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file belongs to the OpenModelica Run-Time System | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), c/o Linköpings | ||
| 5 | * universitet, Department of Computer and Information Science, SE-58183 Linköping, Sweden. All rights | ||
| 6 | * reserved. | ||
| 7 | * | ||
| 8 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE | ||
| 9 | * AGPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. ANY | ||
| 10 | * USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S | ||
| 11 | * ACCEPTANCE OF THE BSD NEW LICENSE OR THE OSMC PUBLIC LICENSE OR THE AGPL | ||
| 12 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 13 | * | ||
| 14 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) Public License | ||
| 15 | * (OSMC-PL) are obtained from OSMC, either from the above address, from the URLs: | ||
| 16 | * http://www.openmodelica.org or https://github.com/OpenModelica/ or | ||
| 17 | * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica distribution. GNU | ||
| 18 | * AGPL version 3 is obtained from: https://www.gnu.org/licenses/licenses.html#GPL. The BSD NEW | ||
| 19 | * License is obtained from: http://www.opensource.org/licenses/BSD-3-Clause. | ||
| 20 | * | ||
| 21 | * This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY | ||
| 23 | * SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF | ||
| 24 | * OSMC-PL. | ||
| 25 | * | ||
| 26 | */ | ||
| 27 | |||
| 28 | #include "meta_modelica.h" | ||
| 29 | #include "meta_modelica_string.h" | ||
| 30 | #include "om_format.h" | ||
| 31 | #include <ctype.h> | ||
| 32 | |||
| 33 | static const MMC_DEFSTRINGLIT(_OMC_LIT_NEG_INF,4,"-inf"); | ||
| 34 | static const MMC_DEFSTRINGLIT(_OMC_LIT_POS_INF,3,"inf"); | ||
| 35 | static const MMC_DEFSTRINGLIT(_OMC_LIT_NAN,3,"NaN"); | ||
| 36 | |||
| 37 | #if defined(_MSC_VER) | ||
| 38 | #include <float.h> | ||
| 39 | #define isnan _isnan | ||
| 40 | #define isinf !_finite | ||
| 41 | #endif | ||
| 42 | |||
| 43 | ✗ | modelica_string _old_realString(modelica_real r) | |
| 44 | { | ||
| 45 | /* NOTE: The RML runtime uses the same code as this function. | ||
| 46 | * If you update one, you must update the other or the testsuite might break | ||
| 47 | * | ||
| 48 | * 64-bit (1+11+52) double: -d.[15 digits]E-[4 digits] = ~24 digits max. | ||
| 49 | * Add safety margin in case some C runtime is trigger happy. */ | ||
| 50 | char buffer[32]; | ||
| 51 | char* endptr = NULL; | ||
| 52 | snprintf(buffer, 32, "%.16g", r); | ||
| 53 | /* If it looks like an integer, we need to append .0 so it looks like real */ | ||
| 54 | endptr = buffer; | ||
| 55 | ✗ | if (*endptr == '-') endptr++; | |
| 56 | ✗ | while (isdigit(*endptr)) endptr++; | |
| 57 | ✗ | if (0 == *endptr) { | |
| 58 | ✗ | *endptr++ = '.'; | |
| 59 | ✗ | *endptr++ = '0'; | |
| 60 | ✗ | *endptr++ = '\0'; | |
| 61 | ✗ | } else if ('E' == *endptr) { | |
| 62 | ✗ | *endptr = 'e'; | |
| 63 | } | ||
| 64 | ✗ | return mmc_mk_scon(buffer); | |
| 65 | } | ||
| 66 | |||
| 67 | 3002308 | modelica_string _ryu_realString(modelica_real r) | |
| 68 | { | ||
| 69 | char str[32]; | ||
| 70 | char buffer[32]; | ||
| 71 | 3002308 | d2s_buffered(r, str); | |
| 72 | 3002308 | ryu_to_hr(str, buffer, 1); /* include the trailing zero for MetaModelica */ | |
| 73 | 3002308 | return mmc_mk_scon(buffer); | |
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | 3002311 | modelica_string realString(modelica_real r) | |
| 78 | { | ||
| 79 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 3002311 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
3002311 | if (isinf(r) && r < 0) |
| 80 | return MMC_REFSTRINGLIT(_OMC_LIT_NEG_INF); | ||
| 81 |
1/2✓ Branch 0 taken 3002311 times.
✗ Branch 1 not taken.
|
3002311 | else if (isinf(r)) |
| 82 | return MMC_REFSTRINGLIT(_OMC_LIT_POS_INF); | ||
| 83 |
2/2✓ Branch 0 taken 3002308 times.
✓ Branch 1 taken 3 times.
|
3002311 | else if (isnan(r)) |
| 84 | return MMC_REFSTRINGLIT(_OMC_LIT_NAN); | ||
| 85 | 3002308 | return _ryu_realString(r); | |
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | #ifdef REAL_STRING_TEST_MAIN | ||
| 90 | #define c 15 | ||
| 91 | int main() | ||
| 92 | { | ||
| 93 | int i,j; | ||
| 94 | double dds[c] = {-1.765,1.765,0,1,4,15e13,1.676e14,-15e-1,0.005,-0.03e13,1234567890,-1234567890,9.999999999999999e22,INFINITY,NAN}; | ||
| 95 | const char *res[c] = { | ||
| 96 | "-1.765","1.765","0.0","1.0","4.0","150000000000000.0","167600000000000.0","-1.5","0.005","-300000000000.0","1234567890.0","-1234567890.0","9.999999999999999e+22","inf","NaN", | ||
| 97 | }; | ||
| 98 | for (i=0;i<c;i++) { | ||
| 99 | for (j=0;j<1;j++) { | ||
| 100 | char *buf; | ||
| 101 | buf = dtostr(dds[i]); | ||
| 102 | fprintf(stderr, "%s%.15g to %s\n", strcmp(res[i],buf)==0 ? "" : "ERRROR: ",dds[i], buf); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | } | ||
| 106 | #endif | ||
| 107 |