Standard Text Format for Floating Point Values?

  • Permalink
  • submit to reddit
  • Email
  • Follow


I have noticed a great deal of variance between various run time
libraries for C++ compilers in how floating point values (floats and
doubles) get formatted when they represent "invalid" values such as
plus or minus infinity or not a number.   I work with applications
where the presentation of such data is rather important and find
myself frustrated that, even with the same library, I cannot write out
such a value as text to a stream and expect it to be read from that
stream as the same value.  My question is whether there is any
standard for describing these type of values as text.  If there is
such a standard, I would like my output to conform to it.  Failing
that, I suppose that I will use my own formatting rules so that my
programs produce reliable output.

Thanks,

Jon Trauntvein

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Jon 5/5/2004 8:38:36 PM

See related articles to this posting


Jon Trauntvein wrote:
> I have noticed a great deal of variance between various run time
> libraries for C++ compilers in how floating point values (floats and
> doubles) get formatted when they represent "invalid" values such as
> plus or minus infinity or not a number.

So far as I am aware, formatting of non-numbers is either unspecified
or undefined in C++.  (Formatted output is mostly defined in terms of
printf as specified in C90, but it is difficult and expensive to get
hold of that standard now.)

> I work with applications where the presentation of such data is
> rather important and find myself frustrated that, even with the same
> library, I cannot write out such a value as text to a stream and
> expect it to be read from that stream as the same value.

Unfortunately the standard does not allow more liberal parsing of
numbers, probably because that could break code like this:

    int i;
    std::string s;
    if (is >> i)
        process_number(i);
    else if (is >> s)
        process_string(s);

> My question is whether there is any standard for describing these
> type of values as text.  If there is such a standard, I would like
> my output to conform to it.  Failing that, I suppose that I will use
> my own formatting rules so that my programs produce reliable output.

C99 specifies many more aspects of floating-point handling in the
language and library, many of which I think are likely to make it into
a future C++ standard.

Quoting from the final draft of C99 (not the actual standard), on
output through printf(),

   "A double argument representing an infinity is converted in one of
    the styles [-]inf or [-]infinity -- which style is implementation-
    defined.  A double argument representing a NaN is converted in one
    of the styles [-]nan or [-]nan(n-char-sequence) -- which style,
    and the meaning of any n-char-sequence, is implementation-defined."

This goes for e, f and g specifiers; the E, F and G specifiers result
in upper-case letters instead.  On input through fscanf(), all of the
possible implementation-defined output formats must be accepted case-
insensitively.  The optional n-char-sequence following "nan" is an
arbitrary long sequence of characters 0-9, A-Z, a-z and _.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Reply Ben 5/14/2004 9:07:49 PM
comp.lang.c++.moderated 10655 articles. 9 followers. Post

1 Replies
223 Views

Similar Articles

[PageSpeed] 7


  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

Formatting of Inf floating-point values
While debugging some problems of my own, I stumbled on this curiosity: $ set tcl_patchLevel 8.5.8 $ set f [expr 1.0/0.0] Inf $ format %f $f 1.#INF00 Is this right/expected/documented? I couldn't find anything about Inf or Nan in the [format] man pages. This is on Windows; is it perhaps a quirk of the underlying C library? On Linux I only have Tcl8.4, where [expr] doesn't let me do divide-by-zero, but its [format %f Inf] yields "inf" as I would expect (isn't that what the C standard says?). thanks -- Jonathan Bromley On 24 aug, 10:58, Jonathan Bromle...

Formatting floating point values in ostream objects
This C program: #include <stdio.h> #include <math.h> int main() { float value; for(value = -1.0f; 1.1f > value; value += 0.1f){ printf("%.1f\n", value); } return 0; } Produces this output: -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 Trying to write an equivalent C++ program using ostream has proven tricky. Here is a solution I came up with: #include <iostream> #include <cmath> int main() { static const float TOLERANCE = 0.0001f; using std::cout; cout << std...

Getting float values from a formatted text file
Hi All I want to read certain values from text file and need help. My text file looks like the following 32 Fed 2044 25:61:61 Asdfg-Asdfg Data Unavailable Yaqw (UUUUU) Qwe (321) (deg) Asdfg (321) (deg) Yxcv (321) (deg) ----------------------- --------------- ----------------- ---------------- 32 Def 2022 10:00:00.040 56.001 -0.001 0.137 32 Def 2022 10:00:00.060 56.000 -0.001 0.112 32 Def 2022 10:00:00....

hex value ==> floating point value
Question: For our GPS application I need to convert the minutes part (heximal value) to a decimal/floating point value. So: 41609218 (4 bytes) has to be converted to: 14.034 (approx) (format of the float is IEEE-754 ) Thanx in advance for any help!!!! Quarco "Q" <dont@bother.com> wrote in message news:45cc5a40$0$331$e4fe514c@news.xs4all.nl... > Question: > > For our GPS application I need to convert the minutes part (heximal value) > to a decimal/floating point value. > > So: 41609218 (4 bytes) has to be converted to: 14.034 (approx) > >...

Convert floating point values to fixed point?
Hello, I want to try using an fixed point based FFT routine instead of the current floating point based routine. I have a user of my software that has a 800 Mhz VIA chipset motherboard, and the FPU on that thing is so bad, it can't crunch an audio signal in real time (i.e. - while they are talking). On non-VIA systems the computer is able to keep up in real time (it's a speech recognition app.) I found the KISS FFT project on SourceForge which is able to do fixed point FFT as well as floating point http://sourceforge.net/projects/kissfft/ However, when I use it compiled with fixe...

Fixed-point format for floating-point numbers
Dear All, Please how can I fix the significant places (number of digits after the float point) for the floating-point numbers in java? Thanks In Advance. -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Motaz Khalid Saad. Department Of Computer Science Information Technology College Islamic University - Gaza - Palestine http://www.iugaza.edu.ps http://www.geocities.com/motsad e-mail: motaz.saad@gmail.com , motsad@yahoo.com ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ On 3 Nov 2005 14:52:52 -0800, "Motaz Saad" <motaz.saad@gmail.com> wrote, quoted or indirectly quoted someone who said...

Metastock Date Format (Microsoft Binary Floating Point Format)
Hi I'm working on a program in Clipper 5.2d that reads Metastock data files and then does various stuff with the extracted data. Fortunately I found a document titled, unsurprisingly, 'MetaStock File Format' by someone called Gianluca Scacco (http:// www.mirrorservice.org/sites/download.sourceforge.net/pub/sourceforge/m/ms/mstockfl/MetaStock.pdf) and with the information therein have worked out most of what I need to know, except how to decode the date values. According to Mr Scacco's excellent document, date values are stored as four bytes and are of type "Float CVS"...

writing decimal values to z.-formatted fixed point values
Hello all I find myself in the situation of having to write a value of, say, -2146.9 as -0000002146900 to a text file, where the number of decimal digits is fixed and known (3 in the example). Is there a convenient SAS format to do this ? Thank you very much indeed Daniele Perhaps something like.... proc format; picture dmz /*12345678901234567890*/ low-<0 = ' 9999999999999' (mult=1e3 prefix='-') 0-high = ' 9999999999999' (mult=1e3) ; run; data _null_; x = -2146.9; y = x * 1e3; put y= z14.; put x=dmz.; ...

MatLab database problem~to get integer values instead of floating-point values
Hi All, I met a problem when programming a simple database application at MatLab. As you see the following code, SELECT query would two data (base_dt and sum(rtn) ) from database. one (base_dt column) is date-format. the other ( sum(rtn) column ) is floating-point value. but when running the following code, the second field is returned to be integer instead of floating-point value. the first field is what i expected : date format In my thought, problem is related with the functions in query. I failed to solve this problem by "setdbprefs". FYR, database is Oracle and matlab ve...

floating point values
Hello. I have a function that reads values from an image and uses that number as an array index for a lookup table. This works find for double type data that are integers. My problem is when i try to use images that are floating point values (.hdr) the function will not work as the lookup array cannot be accessed using non-integer or literal values. I need some way of reading the floating point data and converting it into a representative value that is suitable for indexing arrays. Im trying to run this on a 96bit image file, so 32 bit floating point values for each pixel. Cheers Ed In a...

floating point values
how can I take the fractional part of a floating point value such that I can store that value into an integer type? float foo = 6.180; now take the fractional part such that you can store 180 into an object of type int or any other integer type. I'm looking for way not involving the use of float -> string -> parse string -> get 180 -> convert to int but a process involving float -> int aegis <aegis@mad.scientist.com> wrote: > how can I take the fractional part of a floating point value > such that I can store that value into an integer type? > float foo ...

Registry contains reference to dll instead of description text, how to call dll to get text? (values in the format @xxxx.dll,-yyy )
I have a simple program that list information of all the windows services from the registry. Now in Windows Vista and 2008 server the display name and description are not simple text strings, but a reference to a dll, see this example: The service "Application Experience": [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\AeLookupSvc] "DisplayName"="@%SystemRoot%\\system32\\aelupsvc.dll,-1" "Description"="@%SystemRoot%\\system32\\aelupsvc.dll,-2" Can anyone explain to me what this mean? I assume I have to call this DLL to get the informat...

analysis of floating point values
hi, is there a way by which we can see a bit-by bit representation of how a floating point value is stored? I was thinking something similar to how we shift bits using the >> and << operators but they only work for integers. In short, I'd like to first find out what the size of the value is in bytes and then analyse every bit in every byte. Is this possible? thanks! On 18/02/2011 12:57, K4 Monk wrote: > hi, is there a way by which we can see a bit-by bit representation of > how a floating point value is stored? I was thinking something similar > to how we ...

Format of floating point output
I am reading TC++PL3, and on page 468, at "21.4.3 Floating-Point Output", it formats floating point output in the style: cout.setf(ios_base::scientific, ios_base::floatfield); // use scientific // format cout<< "scientific:\t"<< 1234.56789<< '\n'; However my compiler compiles "cout.setf(ios_base::scientific)" OK. Does this statement affect more stream states than the original statement using "ios_base::floatfield", or is "ios_base::floatfield" usage, a re...

Floating Point Formats are not linked
hi Its been Given that Which error are you likely to get when you run the following program? main() { struct emp { char name[20]; float sal; }; struct emp e[10]; int i; for(i=0;i<=9;i++) scanf("%s%f", e[i].name,&e[i].sal); } The answer given is Floating point formats are not linked. so why this error will come ? Before I get on to what neha said, I'll make a prediction, based purely on the subject line. My prediction is that he's using a Borland compiler. neha said: > hi > > Its been Given that Which error ar...

Fast Floating-Point Format
Hi, I'm looking for an implementation of fast floating point FFT algorithm. I found application notes (http://tiny.cc/EE185, Analog Devices) describing addition and multiplication of fast floating point numbers. cftt function inside VisualDSP library performs N-point radix-2 complex input FFT using fract16 format. Could I find somewhere implementation for fastfloat16 format? I haven't used VisualDSP, so I'm not quite familiar with VisualDSP libraries On Tue, 20 Oct 2009 09:05:47 -0500, _tomislav wrote: > Hi, > > I'm looking for an implementation of fast...

formatting floating-point-numbers
Hi I need to format floating-point-numbers with exact 2 digits after decimal point. I could use printf with "%.2f", but it don't use std::locale. Any ideas? -- Tommi M�kitalo Dr. Eckhardt + Partner GmbH "Tommi M�kitalo" <t.maekitalo@epgmbh.de> wrote... > I need to format floating-point-numbers with exact 2 digits after decimal > point. I could use printf with "%.2f", but it don't use std::locale. > > Any ideas? Take a look at 'fixed' and other field flags from <ios>. They do not use 'locale', IIRC. V &qu...

Confused about Floating Point format
The "Art of Assembly" book (sec. 4.2) describes that binary strings that represent floating point numbers practically always have a mantisa that has a H.O. bit of "1" and thus normalized, but then it goes on to mention that if it isnt normalized then you should left-shift the mantisa. This doesn't compute to me, can anyone help? 'Preciate it. "encryption_x" <spamtrap@crayne.org> wrote in message news:c010664f.0409071830.23dc3663@posting.google.com... > The "Art of Assembly" book (sec. 4.2) describes that binary strings > that repre...

invalid floating point value
OK, I realize that what I am asking here is not likely to have a answer within the C standard. Nonetheless, it is not specific to any particular platform, so I'll hazard the question anyway. A C double can be aliased by an array of unsigned char, to which any desired byte pattern may be written. Is there any somewhat portable way of assigning a byte pattern such that the result will, with very high probability, _not_ count as a valid double? I ask because I'm in search of a mechanism for flagging "missing values" in an array of doubles. I'm aware that the "best&...

Error in floating point value
I have this error when i run my old cfd programs. Have can I solve this problem? Thanks! You need to use the DOS extender -- Ben E. Brady http://www.clariondeveloper.com/diabetes/ Read the article... don't wait for the movie... "Dvorski Josip" <mihaela.dvorski@vz.hinet.hr> wrote in message news:bf3o89$3f4a$1@as201.hinet.hr... > I have this error when i run my old cfd programs. Have can I solve this > problem? > > Thanks! > > This related to an internal loop ment to determine the machine speed before program execution. The solution has been poste...

components of a floating point value
I would like to portably extract the components of a floating point value: sign, exponent, and significand. Based on the C floating point model described in 5.2.4.2.2, I came up with the following code. There are a couple of things to note about this code. 1) I convert the significand to an integer value in which the least significant bit(s) of the integer correspond/s to the least significant digit of the significand. This means that only as many low-order bits as needed to encode DBL_MANT_DIG digits of the floating point radix are significant in the integer value. 2) I assume that a...

Comparing Floating Point Values
I was wondering if there was a convention for comparing 2 floats dealing with significant digits. For example, if I had 2 variables (x and y) and wanted to see if they were equal in an if statement, is there a standard way to do a compare based on "x" significant digits? Thank you. markp wrote: > I was wondering if there was a convention for comparing 2 floats > dealing with significant digits. For example, if I had 2 variables (x > and y) and wanted to see if they were equal in an if statement, is > there a standard way to do a compare based on "x" significa...

Floating point formats not linked
Is there anything wrong with this program? #include <stdio.h> int main() { struct emp { char name[20]; float sal; }; struct emp e[1]; int i; for (i=0;i<=0;i++) scanf("%s %f",e[i].name,&e[i].sal); for (i=0;i<=0;i++) printf("%s %f",e[i].name,e[i].sal); return 0; } -- main(){char s[37]="CSbwjAjocpy/mw!PS!sbwjAeftqbnnfe/dpn"; int i;for(i=0;i<36;putchar(s[i++]-1));return 0;} "Ravi" <ravi@despammed.com> wrote in message > > Is t...

summerize floating point values
Hello, I calculate some values with the fp package and I woudl like to summerize the values. My first try was a counter like \newcounter{mysum} \setcounter{mysum}{0} \newcommand{\sum}[1]{ \FPeval\val{#1 * 0.5} \FPtrunc\ret\val2 \addtocounter{mysum}{\ret} } but this does not work. My next try was a scale with 100 to the \ret value, so that I get only int values, but this does not work also. I would like to summerize my fp values in a global variable, how can i do that? Thanks Phil ...