|
I want to find the number of decimal point. for example 8.995 is the tax amount. I want to count number of digits after decimal point; in this case I want to get 3
|
|
|
|
|
ptr_Electron wrote: for example 8.995 is the tax amount. Which could be stored in memory as 8.9949999999999992 or something similar. Then you would get 16 (or maybe 15 , I forget) as an answer.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
modified 16-Oct-18 9:34am.
|
|
|
|
|
May I know the correct approach please.
|
|
|
|
|
It's still unclear as to what you are trying to accomplish. Once you have the number of digits in the mantissa, then what?
Again, if you would provide details for your end-game, we may be able to offer a better solution. Manipulating floating point numbers on a binary computer is not a trivial task.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Thanks for response. a given decimal number. I would the count of decimal points.
For example
6.67 -? I need 2
6.9876 I need 4
1.67890 I need 5
Count of digits after decimal point
|
|
|
|
|
ptr_Electron wrote:
6.67 -? I need 2
6.9876 I need 4
1.67890 I need 5 Which are stored in memory as:
6.6699999999999999
6.9875999999999996
1.6789000000000001 or something very similar. Notice all the mantissas are the same length?
Is this just a trivial exercise for you, or are you ultimately planning on doing something with the result?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
modified 16-Oct-18 15:02pm.
|
|
|
|
|
As per what David has explained all doubles are actually stored subtly different to what you think because they round. I just want to extend why they round.
They round because the computer works in base 2 (0 and 1's) and you are working in decimal base 10 (0,1,2,..9)
10 does not work as a power of 2 you can go either side 2x2x2=8 OR 2x2x2x2=16 so any base 10 decimal fraction when written in base 2 will likely round
Double-precision floating-point format - Wikipedia[^]
Assuming you are on a standard Microsoft compiler many will be rounded at 52 bits long and we have no way to know what length you actually typed in.
So usually when writing doubles to screen you fix the decimal places
Here is how the standard print function does it the %.3f means take float write to 3 decimal places
printf("Double value: %.3f\n", 3.1234543747321475);
I have made a randomly long value but if you execute it only puts out 3 decimal places.
There are many conversion routines in C/C++ to convert them in fixed decimal places to screen, buffers etc.
So generally you fix the length at display or while the number is in string format, you can't work the problem in reverse the moment it stores the original length is lost.
So basically once stored there is no way to count the decimal places .. you can't do what you asked.
Lastly should add this is nothing to do with C, any language that stores numbers as doubles behaves that way.
In vino veritas
modified 17-Oct-18 10:51am.
|
|
|
|
|
Such code looks flawed.
What is the type of the tax variable?
|
|
|
|
|
Tax variable is of type Double
|
|
|
|
|
Then your code is apparently flawed.
|
|
|
|
|
I'm doing an assignment and it was told that SIZE of the array has to be const and also that I will need to create an assignment operator, but idk maybe my lecturer didn't think about it?
I have this code:
class NameList
{
private:
const int SIZE;
string* mp_list;
...
And then there's assignment operator
NameList NameList::operator = (const NameList &otherList)
{
SIZE = otherList.SIZE;
delete[] mp_list;
mp_list = new string[SIZE];
*mp_list = *otherList.mp_list;
}
This will not work since you can't change const. But it's bad if I don't change the SIZE variable. Is there a way to create an assignment operator with const variable? Or is it ok if the size of the array is not const or it's bad programming? What would you do?
|
|
|
|
|
There could be a lot of guesswork, but, as a matter of fact, I suppose you should ask your Lecturer.
|
|
|
|
|
You need to assign a value to a constant item, otherwise it has no meaning. You need something like:
class NameList
{
private:
const int SIZE = 100; string* mp_list; ...
public:
NameList()
{
mp_list = new string[SIZE];
}
...
From there you should be able to figure out how to make your assignment operator method.
|
|
|
|
|
Since the value of SIZE is constant and the same for all instances of the class, I suggest it be made a static const .
That way you wouldn't need to assign a value to it in the assignment operator overload.
static const int SIZE = <value>;
|
|
|
|
|
For synchronization purpose, I am implementing a wait function that is gonna wait until a package is executed.
I have following code in user application
bool finished;
function(){
int id = createpackage(&finished);wait(&finished)}
Inside the library(i am developing), i am trying to do this
package.cpp
createpackage(bool* finished){
*finished = false;
package pack(function, arguments, finished);
package::package(){
m_packagefinished = *finished;
void package::setHandle(){
lock
unlock
}
}
In another class i am executing the package and want to set the m_finished to true after packge is being executed.
packageinstance.execute();
packageinstance.setHandle();
Wait function(implemented in a different class) is as follows:
wait(bool * finished){
while(*finished == false)
yeild(); }
The problem is that how can i get the correct value of finished inside the wait function.
|
|
|
|
|
|
yeah i am creating a multi threaded application but i am not allowed to use any libraries etc. Its a bare metal implementation so i need to find a way to do it myself. I am using Pthreads just to emulate the physical cores of a system.
|
|
|
|
|
|
In the user application you should declare the finished variable volatile .
In the package.cpp you should capture the pointer (or a reference) to the variable. E.g.
package::package(){
m_pfinished = finished;
void package::setHandle(){
lock
*m_pfinished = true; unlock
}
|
|
|
|
|
|
|
in common.c
typedef unsigned char UN_Char;
typedef enum { Valid = 0, Invalid = 1, Unknown = -999 } AppResponseCode;
Method in SomeApi.C , need to Call from C#
static AppResponseCode SomeFunction(UN_Char ** vUC1PtrPtr,size_t * vSize1Ptr, UN_Char * vUC2Ptr,size_t vSize2, FILE * inputFile, char * vcharPtr)
I tried to add reference of provided C application DLL but Error appear
A Reference to '.....\SomeApi.DLL' could not be added. Please make sure that the file is accessible, and that it is valid assembly or COM Component
So the other option I have is to use DLLImport
[DllImport("SomeAPI.dll", CharSet = CharSet.Unicode)]
public static extern AppResponseCode SomeFunction( ????? ) ;
But I don't know what parameter types I had to pass for Return and Calling parameters to Call and obtain value from SomeFunction()
PS: Though I have source code for Complete C application but I can't re-compile it or modify it, I am only allowed to work with available Exe/DLL
|
|
|
|
|
if you make c file to DLL, is not that okay?
|
|
|
|
|
|
Could anyone tell me how to when I run a c++ program, how to make the screen bigger?
|
|
|
|