473,732 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_b ase::scientific , ios_base::float field); // use scientific
// format

cout<< "scientific:\t" << 1234.56789<< '\n';
However my compiler compiles "cout.setf(ios_ base::scientifi c)" OK.

Does this statement affect more stream states than the original
statement using "ios_base::floa tfield", or is "ios_base::floa tfield"
usage, a redundant explicit statement?
Sep 25 '07 #1
4 2360
john wrote:
>
correction:
I am reading TC++PL3, and
==on page 628,
at "21.4.3 Floating-Point
Output", it formats floating point output in the style:
cout.setf(ios_b ase::scientific , ios_base::float field); // use scientific
// format

cout<< "scientific:\t" << 1234.56789<< '\n';
However my compiler compiles "cout.setf(ios_ base::scientifi c)" OK.

Does this statement affect more stream states than the original
statement using "ios_base::floa tfield", or is "ios_base::floa tfield"
usage, a redundant explicit statement?
Sep 25 '07 #2
On Sep 25, 12:34 pm, john <j...@no.spamwr ote:
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_b ase::scientific , ios_base::float field); // use scientific
// format
cout<< "scientific:\t" << 1234.56789<< '\n';
However my compiler compiles "cout.setf(ios_ base::scientifi c)" OK.
Does this statement affect more stream states than the
original statement using "ios_base::floa tfield", or is
"ios_base::floa tfield" usage, a redundant explicit statement?
Without the second parameter, it might result in an illegal
value in the floatfield, which would result in undefined
behavior.

Most of the format options are boolean values, represented by
single bits. To set a bit, the implementation simply or's the
bit value with the existing value. For example, if you call
std::cout.setf( std::ios::showp os ), the results will be the
previous value, but with the showpos flag unconditionally set.
For such boolean values, the single argument form of setf is
appropriate, and to reset, you would normally use ios::unsetf.

Three of the format values, however, are non-boolean (i.e. they
have more than two possible values): floatfield, adjustfield and
basefield. Because they have more than two values, they
consist of more than one bit, and simple or'ing won't work.
Consider a case, for example, where the floatfield values are
defined as: unnamed default: 0, fixed: 1, scientific: 2. If the
current value is fixed, and you simply or in scientific, the
results will be 3---an illegal value, which may cause strange
things to happen. For these fields, the two argument form of
setf is used; this form first and's the format with the
complement of the second argument, effectively setting all of
the bits from the second argument to 0, before or'ing the first
argument (which is also and'ed with the second). And the values
floatfield, adjustfield and basefield are defined to contain all
of the bits which need to be reset---with the example values
above, the value would be 3.

Note that this two argument form could be used to reset any of
the one bit flags: call it with 0 as the first argument, and all
of the flags to be reset as the second. This is very
unidiomatic, however, and I wouldn't do it. Basically, in well
written code, the following rules apply:

-- The two argument form is always used to set the base, the
floating point format, and the alignment. The second
argument is always one of ios::floatfield , ios::basefield or
ios::alignfield ; the acceptable values for the first
argument depend on the second argument (and may be 0, cast
to the type fmtflags, although unsetf can also be used in
this case). The two argument form is used to set one field
at a time; although it's quite possible to set more, it's
quite unidiomatic, which renders the code more difficult to
read.

-- The one argument form of setf is used to set any of the
other values, and unsetf is used to reset them. Unlike the
case of the two argument form, it's quite idiomatic to
combine values here, e.g. to call setf with something like
"ios::showp oint | ios::showpos".

-- The "easiest" way to do complicated manipulations on the
flags is to read them, using ios::flags(), manipulate your
local fmtflags variable, then set the flags to the resulting
value (using the non-const std::flags()). This is often
used as well because you want to save the original value,
and restore it when you're through.

I use the third solution in my custom manipulators (which
restore the original flags in their destructor); for some
examples of this, you might want to look at the StateSavingMani p
and *Fmt in IO sub-system of my library
(http://kanze.james.neuf.fr/code-en.html). (Note that most
application code should not manipulate the flags directly, nor
use the standard manipulators, but use rather application
specific manipulators.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 26 '07 #3
James Kanze wrote:
>
Most of the format options are boolean values, represented by
single bits. To set a bit, the implementation simply or's the
bit value with the existing value. For example, if you call
std::cout.setf( std::ios::showp os ), the results will be the
previous value, but with the showpos flag unconditionally set.
For such boolean values, the single argument form of setf is
appropriate, and to reset, you would normally use ios::unsetf.

Three of the format values, however, are non-boolean (i.e. they
have more than two possible values): floatfield, adjustfield and
basefield. Because they have more than two values, they
consist of more than one bit, and simple or'ing won't work.
Consider a case, for example, where the floatfield values are
defined as: unnamed default: 0, fixed: 1, scientific: 2. If the
current value is fixed, and you simply or in scientific, the
results will be 3---an illegal value, which may cause strange
things to happen. For these fields, the two argument form of
setf is used; this form first and's the format with the
complement of the second argument, effectively setting all of
the bits from the second argument to 0, before or'ing the first
argument (which is also and'ed with the second). And the values
floatfield, adjustfield and basefield are defined to contain all
of the bits which need to be reset---with the example values
above, the value would be 3.

Note that this two argument form could be used to reset any of
the one bit flags: call it with 0 as the first argument, and all
of the flags to be reset as the second. This is very
unidiomatic, however, and I wouldn't do it. Basically, in well
written code, the following rules apply:

-- The two argument form is always used to set the base, the
floating point format, and the alignment. The second
argument is always one of ios::floatfield , ios::basefield or
ios::alignfield ; the acceptable values for the first
argument depend on the second argument (and may be 0, cast
to the type fmtflags, although unsetf can also be used in
this case). The two argument form is used to set one field
at a time; although it's quite possible to set more, it's
quite unidiomatic, which renders the code more difficult to
read.

-- The one argument form of setf is used to set any of the
other values, and unsetf is used to reset them. Unlike the
case of the two argument form, it's quite idiomatic to
combine values here, e.g. to call setf with something like
"ios::showp oint | ios::showpos".

-- The "easiest" way to do complicated manipulations on the
flags is to read them, using ios::flags(), manipulate your
local fmtflags variable, then set the flags to the resulting
value (using the non-const std::flags()). This is often
used as well because you want to save the original value,
and restore it when you're through.

I use the third solution in my custom manipulators (which
restore the original flags in their destructor); for some
examples of this, you might want to look at the StateSavingMani p
and *Fmt in IO sub-system of my library
(http://kanze.james.neuf.fr/code-en.html). (Note that most
application code should not manipulate the flags directly, nor
use the standard manipulators, but use rather application
specific manipulators.)

I suppose you meant "ios_base:: " where you wrote "ios::".
Sep 26 '07 #4
On Sep 26, 11:25 am, john <j...@no.spamwr ote:
James Kanze wrote:
[...]
I suppose you meant "ios_base:: " where you wrote "ios::".
Not really. I learned iostreams back with the classical
iostreams, when there was only one base class, ios, rather than
basic_ios<>, deriving from ios_base, and I can never remember
how things got divided up: what went into basic_ios<>, and what
went into ios_base. But it doesn't matter, since everything in
ios_base is also visible in basic_ios<>. So if I'm writing a
template, I'll use basic_ios< charT, traitsT >, but most of the
time, I'll just use ios (or wios), which is a typedef for
basic_ios< char, char_traits< char .

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 27 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
30162
by: J | last post by:
Would anyone know if there a type tag to format a double? I have f for floating point, but cannot find one for double.
23
8740
by: Matt Garman | last post by:
Is there a clean, portable way to determine the maximum value of converted numerical fields with printf()-like functions? Doing this at compile-time would be preferable. For example, %i should never convert to a string that is longer than the number of digits in INT_MAX, right? I'd like to create a structure that contains character representations of numerical types (e.g., instead of storing 3421 as an int, it would store the char...
15
3932
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this the EPSILON? I know in float.h a FLT_EPSILON is defined to be 10^-5. Does this mean that the computer cannot distinguish between 2 numbers that differ by less than this epsilon?
5
5345
by: sankar | last post by:
Hi, I am using a Q14.18 value. There are tables used in my program which are being indexed by the exponent and mantissa parts of the corresponding floating point value. So how can I get the exponent and mantissa parts of a floating point number from its Q format representation.
33
2769
by: dis_is_eagle | last post by:
hi....i have encountered strange problem regarding floating point comparison...the problem is... main() { float a=0.7; if(0.7 a) printf("hi"); else printf("hello");
4
2153
by: John Friedland | last post by:
'printf' has a '%a' conversion for floating-point output: For example, printing '123456' with "|%13.4a|" produces | 0x1.e240p+16| I've looked through Josuttis and the header files, but I can't find any flags or manipulators that could handle this. Is this possible with stream I/O?
1
1196
by: Duncan Muirhead | last post by:
I'm writing a wee utility that applies some simple transforms (eg adds offsets) to some (text file) fields that represent floating point numbers. Simple enough. However I would like to preserve the input format, e.g. if the input looks like %15.7e then so should the output. Is there any cunning use of e.g. sscanf to get not just the number, but also the format? TIA Duncan
3
6219
by: kimi | last post by:
hi all, can anyone tell me how to convert a comp-1 floating point number to a decimal in REXX??
18
2835
by: n.torrey.pines | last post by:
I understand that with floats, x*y == y*x, for example, might not hold. But what if it's the exact same operation on both sides? I tested this with GCC 3.4.4 (Cygwin), and it prints 0 (compiled with - g flag) #include <cmath> #include <iostream> int main() {
0
8774
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.