473,468 Members | 4,540 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Float and double, precision and scale??


If someone defines a float and double in C++, does that say anything
about how many digits the number will hold at maximum and how many
places there will be after the decimal point?

Or is this open to interpretation?

--
Posted via http://dbforums.com
Jul 19 '05 #1
3 108635
Erik2000 wrote in news:30****************@dbforums.com:

If someone defines a float and double in C++, does that say anything
about how many digits the number will hold at maximum and how many
places there will be after the decimal point?

Or is this open to interpretation?


The class template std::numeric_limits< Type >

defined in <limits> has a whole bunch of static members
that describe the properties of C++'s numeric types.
Here's an example:

#include <iostream>
#include <ostream>
#include <limits>

int main()
{
typedef std::numeric_limits< double > dl;
typedef std::numeric_limits< float > fl;

using namespace std;

cout << "double:\n";
cout << "\tdigits (bits):\t\t" << dl::digits << endl;
cout << "\tdigits (decimal):\t" << dl::digits10 << endl;

cout << endl;

cout << "float:\n";
cout << "\tdigits (bits):\t\t" << fl::digits << endl;
cout << "\tdigits (decimal):\t" << fl::digits10 << endl;
}

I get:

double:
digits (bits): 53
digits (decimal): 15

float:
digits (bits): 24
digits (decimal): 6

on my system.
HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #2
Erik2000 wrote in news:30****************@dbforums.com:
double:
digits (bits): 53
digits (decimal): 15

float:
digits (bits): 24
digits (decimal): 6

on my system.

So does this mean that a double on your system can have up to 53 digits


No the 53 is bits, computers (well most computers) store floating
point values in binary, base 2 not base 10 (decimal).

The 15 is the number of decimal digits. i.e. how meny decimal
digits can be stored in 53 bits.
and 15 digits behind the decimal point?
The decimal point doesn't come in to it, hence the term
*floating-point*.

for example the number 2.3e20 doesn't really have "point" it is
230000000000000000000, also the number 2.3e-20 is
0.0000000000000000023, i.e. the "point" isn't within the digits
at all.

And for float, 24 digits and 6 digits behind the decimal point?


As above 6 decimal digits total or 24 bits (base 2 digits).
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #3


Erik2000 wrote:
double:
digits (bits): 53
digits (decimal): 15

float:
digits (bits): 24
digits (decimal): 6

on my system.
So does this mean that a double on your system can have up to 53 digits
and 15 digits behind the decimal point?


No it means, that a double uses 53 bits as its mantissa.
In decimal this gives an average precission of 15 digits.

And for float, 24 digits and 6 digits behind the decimal point?
24 bits for the mantissa which results in usually
6 decimal digits.

Note: 100000.4
already uses 7 decimal digits and thus cannot be represented proerly
with this float. (Just count the digits and ignore the decimal point).

-> Conclusion: float isn't big enough most of the time to do serious
calculations, at least not in a technical applications. So forget
about float until you know what you do.

You might also want to check out:

http://docs-pdf.sun.com/800-7895/800-7895.pdf

53 digits is a really big number.
54 is bigger :-)

--
Posted via http://dbforums.com


--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #4

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

Similar topics

1
by: Giovanni Azua | last post by:
Hello all, I need to return back to TSQL two numeric values from an Extended Stored Procedure developed in C. As result my C dll program produces two float values, the TSQL side expects to have...
6
by: spasmous | last post by:
Hi, I'm running out of memory storing a large float array. One acceptable workaround would be to reduce the float precision by switching from the default, which is 4 bytes long, to a smaller one -...
8
by: vijay | last post by:
Hello, What happens to float variable in loops. For example, float f=8.7; if(f<8.7) printf("less"); else if(f==8.7) printf("equal"); else if(f>8.7)
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
8
by: abdul_n_khan | last post by:
Hello, I have a basic question related to datatypes. I am trying to read a value using Microsoft's ADO recordset from a field (lets call it 'Price') with datatype decimal(19,6) => 19 = Precision,...
60
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
9
by: richard_lavoie | last post by:
Hi, I have something like this: vector<floatvec1; and I want to cast it, so I use vector vec2<double= static_cast< vector<double(vec1); I always become a error: syntax error before `>'...
11
by: Ole Nielsby | last post by:
First, sorry if this is off-topic, not strictly being a C++ issue. I could not find a ng on numerics or serialization and I figure this ng is the closest I can get. Now the question: I want...
6
by: Jeff Kish | last post by:
Greetings. I need to convert some columns of type numeric(12, 0) to hold floating point information scale and precision I can't determine in advance (customer data can vary wildly) so I wanted...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.