473,473 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

length of double???

5 New Member
I'm sort of new to c++ but i do have years experience in other languages. So if this is a rediculous question and im just not seeing the obvious answer, don't reserve yourselves, bring on the bluntness

So here my question...

Like you may know, there are functions for finding the length of a string (myString.length()), but is it possible to find the length of a double?? More specifically if i have myDouble = 4.53 then i guess the length would be 3.

Example:

Expand|Select|Wrap|Line Numbers
  1. void myFunction(string inputString)
  2. {
  3. double tmpDbl;
  4.  
  5. //convert string to float
  6. tmpDbl = atof(inputString.c_str());
  7.  
  8. this->myDouble = tmpDbl;
  9.  
  10. //I NEED HELP HERE!!!
  11.  
  12. this->myLength = ??? (the length of tmpDbl)
  13.  
  14. }
Apr 30 '09 #1
6 37161
whodgson
542 Contributor
As far as I know it is not 3.
Use the sizeof() function and it will tell you the width in bits (probably 8 on a 32 bit system)
hth's
Apr 30 '09 #2
JosAH
11,448 Recognized Expert MVP
A double number always takes eight bytes, no matter whether you store a simple 1.0 or 1.23456789; the number is stored in the IEEE754 format and has its peculiarties. In the first article of this group is a link to an article that explains everything about floating point format numbers and their inaccuracies and everything.

kind regards,

Jos
Apr 30 '09 #3
donbock
2,426 Recognized Expert Top Contributor
Are you asking about the physical length of a double? As already reported, the physical length (which is more properly termed the 'size') is not affected by the value of the double variable.

Are you asking about a logical length of a double -- something like the number of significant digits? That's an interesting question. I can think of situations where keeping track of the number of significant digits would be helpful. I can't think of any way to extract that information from a double variable. Take your example: you assign the value 4.53 to a double variable, but due to floating-point imprecision the value actually stored in your variable is 4.5299999879. How can you recognize that the number of significant digits of that actual value is 3?
Apr 30 '09 #4
kgkgkg
5 New Member
First, I want to thank you all, you all are awesome and I'll take all of your advice and keep track of the amount of bytes.

Second, about the question that i asked, just for reference, and to clarify a bit on what i was asking:

I know that in c# and vb.net i can take a string variable and measure it's length or size (don't know if I'm using the right terminology). for example

Expand|Select|Wrap|Line Numbers
  1. C#
  2.  
  3. int stringLength;
  4. string myString = "hello kyle!";
  5.  
  6. stringLength = myString.Length();
So stringLength should now hold the value of 11, because the Length() function counts all the ascii characters that myString is made up of.

So now if i had the same example as above, but instead of using a String, used a Double.

Expand|Select|Wrap|Line Numbers
  1. C#
  2.  
  3. int dblLength;
  4. double myDbl = "12345";
  5.  
  6. dblLength = myDbl.Length();
I know that there isn't a member function for double that gives it's length back (the count of ascii characters), but i want to check if there were any other functions that any of you know of that would count up the characters in myDbl.

--Kyle
Apr 30 '09 #5
donbock
2,426 Recognized Expert Top Contributor
@kgkgkg
Notice that the value you assign to myDbl is enclosed in double quotes. That makes it a string. I don't know about C#, but C does not let you assign a string value to a double variable. However, you could do something like this:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. int valueLength;
  3. double valueFP;
  4. const char * const valueString = "12345";
  5. valueFP = strtod(valueString, NULL);
  6. valueLength = strlen(valueString);
but it would only work with literal constants, not the results of computation. I don't really see much value in jumping through all these hoops.

How would you use dblLength if you could get it?
Apr 30 '09 #6
kgkgkg
5 New Member
I didn't mean to put double quotes around the value, what I meant to do was

Expand|Select|Wrap|Line Numbers
  1. myDbl = 12345;
sorry about that. That's what happens when i copy past code, I butcher it, I always forget to change something.

But the scope: It would be in a class that would be able to take a string value or a double in the constructor, which would convert the string value to a double, and that is how it would be stored internally. But after posting this and talking about it with you guys, I've come to the conclusion that it would just be easier to store the values internally as a string. The advantages being that i could get the length of the string, then i could always just convert to a double when needed for output.

Thank you for all of your input!

--kyle
Apr 30 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Kelvin | last post by:
Hi: I have double d = 0.33 when I try to display this using: cout << d; it gives me 0.3329999999999999 i know it's caused by the way that computer stores float number... but how can i get...
16
by: steflhermitte | last post by:
Dear cpp-ians, I am working with a structure: struct meta_segment { long double id; long double num; long double mean; bool done;
5
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h>...
32
by: Carson | last post by:
Hi , Is there a very efficient way to set a double array to 0 ? (I have tried memset, but the result doesn't look correct.) Carson
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
4
by: marco_segurini | last post by:
Hi, From my VB program I call a C++ function that gets a structure pointer like parameter. The structure has a field that contains the structure length and other fields. My problem is that...
3
by: Little | last post by:
Could someone help me get started on this program or where to look to get information, I am not sure how to put things together. 1. Create 4 double linked lists as follows: (a) A double linked...
1
by: Little | last post by:
Could someone help me figure out how to put my project together. I can't get my mind wrapped around the creation of the 4 double Linked Lists. Thank your for your insight. 1. Create 4 double...
3
by: Little | last post by:
Could someone tell me what I am doing wrong here about declaring mutiple double linked lists. This is what the information is for the project and the code wil be below that. Thank your soo much for...
1
by: JWest46088 | last post by:
I keep getting these error messages: area(double,double) in Rectangle cannot be applied to () return "Area: " + Rectangle.area() + "\tCircumference: " + Rectangle.perimeter(); ...
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
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.