MantisBT - Open CASCADE
View Issue Details
0031515Open CASCADE[OCCT] OCCT:Foundation Classespublic2020-04-23 13:442020-09-18 07:38
sshutina 
nds 
lowfeature 
closedno change required 
 
 
0031515: Incorrect conversion Standard_Real to TCollection_AsciiString and TCollection_ExtendedString
Standard_Real is converted to TCollection_AsciiString or TCollection_ExtendedString via the constructor TCollection_AsciiString::TCollection_AsciiString (const Standard_Real value) or TCollection_ExtendedString::TCollection_ExtendedString (const Standard_Real value).
The result is the string of the length no more than 7 digits.
TCollection_AsciiString (const Standard_Integer value) and TCollection_ExtendedString (const Standard_Integer value) haven't the limits of the number of the digits.

Output:
Standard_Real: 123456.7 TCollection_ExtendedString : 123457 Lenght = 6
Standard_Real: 12345.67 TCollection_ExtendedString : 12345.7 Lenght = 7
Standard_Real: 12.34567 TCollection_ExtendedString : 12.3457 Lenght = 7
Standard_Integer: 1234567 TCollection_ExtendedString : 1234567 Lenght = 7
Standard_Real: 12345.6 TCollection_ExtendedString : 12345.6 Lenght = 7
Standard_Integer: 123456789 TCollection_ExtendedString : 123456789 Lenght = 9
Standard_Real: 12345.6789 TCollection_ExtendedString : 12345.7 Lenght = 7

Standard_Real: 123456.7 TCollection_AsciiString : 123457 Lenght = 6
Standard_Real: 12345.67 TCollection_AsciiString : 12345.7 Lenght = 7
Standard_Real: 12.34567 TCollection_AsciiString : 12.3457 Lenght = 7
Standard_Integer: 1234567 TCollection_AsciiString : 1234567 Lenght = 7
Standard_Real: 12345.6 TCollection_AsciiString : 12345.6 Lenght = 7
Standard_Integer: 123456789 TCollection_AsciiString : 123456789 Lenght = 9
Standard_Real: 12345.6789 TCollection_AsciiString : 12345.7 Lenght = 7

In the code
// -------------------------------------------------------------------------
// Create an asciistring from a real
// -------------------------------------------------------------------------
TCollection_AsciiString::TCollection_AsciiString(const Standard_Real aValue)
     : mystring(0)
{
  char t [50];
  mylength = Sprintf( t,"%g",aValue);
  mystring = Allocate(mylength+1);
  memcpy (mystring, t, mylength);
  mystring[mylength] = '\0';
}

the function Sprintf(...) returns the incorrect length and the incorrect array of the characters.
No tags attached.
Issue History
2020-04-23 13:44sshutinaNew Issue
2020-04-23 13:44sshutinaAssigned To => nds
2020-04-23 15:06sshutinaDescription Updatedbug_revision_view_page.php?rev_id=22850#r22850
2020-04-23 15:07sshutinaDescription Updatedbug_revision_view_page.php?rev_id=22851#r22851
2020-04-23 15:07sshutinaDescription Updatedbug_revision_view_page.php?rev_id=22852#r22852
2020-04-23 15:15sshutinaDescription Updatedbug_revision_view_page.php?rev_id=22853#r22853
2020-04-23 15:26sshutinaDescription Updatedbug_revision_view_page.php?rev_id=22854#r22854
2020-04-23 19:17ndsRelationship addedrelated to 0031516
2020-04-23 19:31kgvNote Added: 0091751
2020-04-23 19:41ndsPrioritynormal => low
2020-04-23 19:41ndsSeverityminor => feature
2020-09-18 07:38abvStatusnew => closed
2020-09-18 07:38abvResolutionopen => no change required
2020-09-18 07:38abvTarget Version7.5.0 =>

Notes
(0091751)
kgv   
2020-04-23 19:31   
This is by design - default string constructor with real numbers is just expected to be used for printing messages, so that it relies on %g for "smart compact real representation".

Code which requires controlling floating point precision should use Sprintf() or similar and pass it to TCollection_AsciiString as string, if needed.