|
Sarath.<fon wrote:<="" div="">can't we use mod (%) instead?
I suppose it is not faster.
|
|
|
|
|
double x(123.3)
ceil(log10(x)) => 3 ie the # digits left of decimal.
I'm still working of # digits right of decimal...
Regards,
Kylur
|
|
|
|
|
Be careful of the behaviour if x = 0 when using "log10".
For the number of digits after "." I think you're going to have some problems, simply because the computer representation of floats and doubles of numbers do not exactly correspond to the value you gave. For instance it could happen that 123.3 actually corresponds to 123.29999999999999 as soon as it is stored as a float. So trying to guess the number of digits after "." makes no sense. You could maybe get around this if you limit the possible amount of decimal numbers to a well chosen value.
|
|
|
|
|
you're going to run into floating-point precision errors, unless you round numbers to a maximum number of decimal places. ex. you're going to come across something like 1.9999999999...
Cleek | Image Toolkits | Thumbnail maker
|
|
|
|
|
String operations are your best bet for the most accuracy and flexibility (easy to control the number of significant figures). Since you don't want to go that route, this might get you started:
To count left side of decimal
<br />
unsigned long countLeftOfDecimal(double x)<br />
{<br />
if (1 > abs(x))<br />
{<br />
return 0;
}<br />
else if (1 == abs(x))<br />
{<br />
return 1;
}<br />
else<br />
{<br />
return ceil(log10(abs(x)));<br />
}<br />
}<br />
<br />
unsigned long countRightOfDecimal(double x)<br />
{<br />
long y = x;
double x1 = x - (double)y;
long count = 0;<br />
while (0.0 != x1)<br />
{<br />
double x2 = x1 * 10.0;
y = x2;
x1 = x2 - y;
++count;<br />
}<br />
return count;<br />
}<br />
Keep in mind that you will run into problems when you have numbers like 1.99999999999... with this algorithm -- which is why converting to a string for counting significant figures is a better solution, and may actually be faster than any non-string solution you come up with that doesn't have problems with infinite repeating decimals.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
-- modified at 15:16 Wednesday 28th June, 2006
|
|
|
|
|
I don't think there is an easy solution for this without passing the real values to a string. This is mainly due to the fact that the real numbers may have an exact representation as decimal and an inexact representation as native. And, of course, vice-versa.
For example, float 0.2 has an inexact native representation. The exact representation is 0.200000002980232. But the resolution present in a float is not enough for all the decimals.
This just means that you probably won't have much luch analysing the bits in the float (or double). Their memory layout can be found in http://www.psc.edu/general/software/packages/ieee/ieee.html[^].
In any case, finding the values you want by using strings or not, this can always get complicated:
float f=0.00031233478376329384729344178300483284723740923762730534976862e7f;
double d=f;
printf("f=%f, d=%.15g", f, d);
This code compiles, but due to the lack of precision in the floating point representations the results are not what one would expect.
So, the only fast solution I can imagine involves the assumption that you have not that many number variations, they are always around zero (controlled exponent), and that you do not have rounding errors. With these assumptions you could build a fast look up table.
For example, assume that you will have numbers between -999.99 and +999.99. This results in 199998 diferent numbers plus the zero, i.e., 199999. So you could program the following algorithm:
// build table
float f;
int i;
i=0;
for(f=-999.99; f<=-999.99; f+=0.01f) {
table[i].left=ComputeLeftByUsingStrings(f);
table[i].right=ComputeRightByUsingStrings(f);
i++;
}
// get the left/right values for float f fast!
f*=100.0f; // shift so that the smallest decimal becomes an integer
int i=(int)f; // convert to int to use as offset to the table
i+=99999; // correct the start offset to start at 0 for the most negative number
int left=table[i].left; // left will contain the number of characters to the left
int right=table[i].right; // right will contain the number of characters to the right
I hope this helps!
Rilhas
|
|
|
|
|
Hi people, I need your help again
Im working with Memory mapped files for the first time.
I need to open a file map, and then go trought each byte of the mapped file and add one to it...
so...
<br />
<br />
LPVOID buffer = NULL;<br />
<br />
buffer = MapViewOfFile(hFileMappingObject, dwDesiredAccess | FILE_MAP_WRITE, dwFileOffsetHigh, dwFileOffsetLow, dwNumberOfBytesToMap);<br />
<br />
LPBYTE buf2 = (LPBYTE)buffer;<br />
<br />
for(SIZE_T i = 0; i < dwNumberOfBytesToMap; i++ ) {<br />
(buf2[i])++;<br />
}<br />
<br />
well, that is exploding my test application in uggly ways.
Any suggestion is welcomed
Thanks in advance
|
|
|
|
|
When you say 'exploding' can you be a bit more specific? Are you getting a valid 'buffer' back or is it NULL? Is the error itself a read access violation, or a write violation? Is it the first value of 'i' that errors, or one of the later ones? What's 'dwDesiredAccess' - does it contain 'FILE_MAP_READ'? How are you getting hFileMappingObject? Is the file you're modifying read only?
The code looks sensible enough to me there, so I suspect it'll be something outside of the snippet you've posted there
Matt Godbolt
Engineer, ProFactor Software
StyleManager project
|
|
|
|
|
It hangs up in the first i value, but I tried reading and it works fine.
So the problem is when I try to write the filemap...
Is strange, because I'm in a windows hook, and the filemap is beign oppened by the notepad, who provides read/write access to the file.
I'll check filemap access rights
Thanks for the feedback Matt
|
|
|
|
|
(buf2[i])++;
looks suspecious. Check two things:
1) What MapViewOfFile() returned is the starting address of a memory chunk in your address space. You may not be able to apply [i] to buf2.
2) If the memory is not initialized, you cannot apply ++ to its content.
Best,
Jun
|
|
|
|
|
If I read buf2 contents, I can see the contents of the file.
When I try to write in buf2, the program hangs up.
What is the method to read some specific data of the file mapping?
Let me explain me better, if I have a CFile object I can use Seek method to move around.
In a Memory mapped file, how can I do that?
|
|
|
|
|
Solved at last, I added PAGE_READWRITE access rights to CreateFileMapping.
I dont understand how a progran can save the changes in the document if it hasn't write rights...
Now the problem, is that whatever I change, is changed in the file too.
I need to keep working.
Thanks to all for the feedback
|
|
|
|
|
Using SHBrowseForFolder not possible have ComboBox and Edit box
if any Directory is selected then both Edit box and combo box are Being filled with Searched directories.
|
|
|
|
|
|
Hi,
I would like to write PushSource filter. This filter should allow the user to pass the single sample with method defined in interface. The problem is that this source filter should be universal, I mean capable to connect with any other filter. For example if I add into the graph a new decompressor that we haven't known before our source filter should connect to it and pass the samples. Does anyone have any idea how to do this? Or the only possible way is to set a lot of possibilities in GetMediaType?
|
|
|
|
|
For every mediatype you support, your source filter must be able to output that media type. If you want it to connect to a decompressor which supports a media type you don't know about, how do you output the samples it's expecting then?
Ok I think I know what you mean now. In that case you just have to provide another interface method that allows the user to set the media type of the sample that he's going to send. He would have to set the media type of the sample before the rest of the graph is built.
-- modified at 9:26 Wednesday 28th June, 2006
|
|
|
|
|
Thanks.
I was considering it before but to the last moment I wasn't sure if it is good idea.
|
|
|
|
|
I search a free book about Design Patterns in C++. If you know some please send me the link.
Thanks.
-:KNOX:-
|
|
|
|
|
|
|
Please say me what is the difference betweem typename and class in templates ?
Thanks.
-:KNOX:-
-- modified at 9:11 Wednesday 28th June, 2006
|
|
|
|
|
AFAIK one difference is that
1)'typename' can only be used in template definitions.
2) 'class' - could be for both class and template definitions.
Somethings seem HARD to do, until we know how to do them.
_AnShUmAn_
|
|
|
|
|
I think they both have the same effect when are used in template declaration:
template< class T > ...
can be replaced with:
template< typename T > ...
But typename keyword has one more meaning when is used within the declared template class.
-- modified at 9:32 Wednesday 28th June, 2006
|
|
|
|
|
Thanks for the help.
-:KNOX:-
|
|
|
|
|
When dealing with templates, there is no difference between them.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|