473,385 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 108624
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...

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.