|
How do I convert a CString to a float without losing any precision? When I do the following I lose precision.
CString strFloat = _T("0.5");<br />
char buf[20];<br />
for(int i=0; i<20; i++)<br />
buf[i] = NULL;<br />
<br />
sprintf(buf, "%s", strFloat);<br />
float nResult = atof(buf);<br />
Thanks,
-Eric
|
|
|
|
|
you can do it like this
CString str(_T("0.5"));
float fResult = atof(str);
|
|
|
|
|
ashxly wrote:
you can do it like this
CString str(_T("0.5"));
float fResult = atof(str);
Nope, get a compile error i I try that. I have also tried _wtoi and _wtol but they lose precision also.
-Eric
|
|
|
|
|
Consider atof(theString.GetBuffer(0)).
Kuphryn
|
|
|
|
|
kuphryn wrote:
Consider atof(theString.GetBuffer(0)).
Nope, I get the same error with this. Can't convert CString to const char *.
There has to be a way to convert a CString to a float without losing the precision. It shouldn't be this hard I wouldn't have thought.
-Eric
|
|
|
|
|
Consider const_cast.
Kuphryn
|
|
|
|
|
What error are you getting? The code provided by ashxly should work. You should get a compiler warning that says
warning C4244: 'initializing' : conversion from 'double' to 'float', possible loss of data
That is because atof returns a double...the warning will go away if you change float fResult to double fResult. OR cast the return value from atof to float.
_wtoi and _wtol are no good for your purpose...they will both truncate to an integer.
Gary Kirkham
A working Program is one that has only unobserved bugs
|
|
|
|
|
CString str(_T("0.5"));
float fResult = atof(str);
the code work well.
certainly , if you use UNICODE,
you'd better use wtof(str) instead
can you tell me the error info?
|
|
|
|
|
What about
float fResult=atof((LPCTSTR)str);
~RaGE();
|
|
|
|
|
;)Rage is right This should dp the work work.
cheers
Himanshu
|
|
|
|
|
Rage wrote:
float fResult=atof((LPCTSTR)str);
This gives the following error:
error C2664: 'atof' : cannot convert parameter 1 from 'const unsigned short *' to 'const char *'
I apologize ahead of time if this makes a difference, but I am compiling this under PocketPC 2002 SDK.
After working somemore lastnight I figured out part of the issue. In my orginal code the call to sprintf() is always returning zero into buf. It works just fine if the user has an whole number in strFloat, but once they enter a decimal number it blowsup, only returning zero into buf.
Thanks for all the help folks. I honestly didn't think this would be that difficult.
-Eric
|
|
|
|
|
The problem is you are working with UNICODE strings and trying to use the ANSI conversion routine. That is just silly. As people have already said, use the UNICODE conversion routine "wtof".
float fResult = wtof (str);
That will work just fine.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Tim Smith wrote:
The problem is you are working with UNICODE strings and trying to use the ANSI conversion routine. That is just silly. As people have already said, use the UNICODE conversion routine "wtof".
float fResult = wtof (str);
That will work just fine.
Tim Smith
Tim,
Thanks for the suggestion. The wtof function works fine, but it truncates the value to a whole number. I lose the precision after the decimal if I use this function. I need a value going out only to the tenths, but I do need to keep the one decimal place.
-Eric
|
|
|
|
|
If I open up an image in an MFC application, how can I get the pixel value of the picture that the mouse is pointing at?
|
|
|
|
|
get current mouse pointer position, and then call
COLORREF color = GetPixel(hdc,x,y);
the value of color is what you want.
it's easy , is it?
good luck
|
|
|
|
|
I am developing a 3D program with DirectX 7.0 SDK. When I have rendered some objects in a 3D scene, I need pick the oject accurately in the scene via my mouse position on the screen. I need your help, and in the fact, I need some source codes. Help!
stanley
|
|
|
|
|
Basically, you can compute the equation for a line between the eye and the near plane at the X,Y location that the user clicked. Then shoot the ray from the eye through the near plane and see what object it intersects.
There are many other methods around. Places like http://www.gamedev.com/ and http://www.flipcode.com should have information on how to do this. Common problem in games.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
I've searched google groups but all I find is "that cant be done". Well, of course it can - and one of you know how to do it.
When you map a network share as a drive, it will be called
[sharename] on [servername] ( n: )
- by default. (n being the drive letter)
If you select properties on a mapped drive, you can change that label to anything, e.g. to "documents" in the "Label:" edit field. (only on w2k, winXP), but since I map my drives from a batch-file being run at each startup, I wan't to change that name programatically. All vb-script offers (don't laugh, please...;P) is ShareName and VolumeName, which are both read-only and respectively e.g. \\server\share and "datadrive2" (that is, the name of the harddisk-partition on the server... why the heck is that visible ???) - so I probably have to write a small exe that can do the job.
Has anybody tried this ? Or do anybody have a good idea how to find out how it's done ? Maybe I'm just searching the wrong places...
Well, any help will be much appreciated - thanks a lot in advance.
/Jan
|
|
|
|
|
I have two signals (for example two voice signals) . And i need to put them one by one, and smooth (or interpolate) possible differences between the end of first signal, and start of another one ..
How can i handle with it ?
Thanks in advance .
|
|
|
|
|
That depends on the relationship between the two signals. Are they partially overlapped, sequential with a fixed or variable delay between them, or sequential with no delay (first sample of a given stream is one sample after the last sample of the prior stream)?
|
|
|
|
|
It's most close to the "partially overlapped" signals ..
so what can you propose to me ?
thanks
|
|
|
|
|
The answer is simple, but I don't want to lead you down the wrong path. More information is needed to determine the correct solution. When you say it's "most close" to partially overlapped, what do you mean? You said this is DSP, so I assume that you're receiving chunks of buffer from an input source. If that is the case, you shouldn't need to worry about overlap or delay between the buffers, and a blunt join of the buffers would be appropriate and wouldn't cause a pop in the audio stream since the two buffers are chunks of a uniform stream. If you have two audio files from independent sources which you wish to connect, or wish to transition from one live audio source to another while receiving data streams from both, then do a linear crossfade as Tim Smith indicated. At the beginning of the fade, the audio source to be faded out is at full volume, the one to be faded in is at zero volume. At the midpoint of the fade, each audio source is at half-volume. At the end of the fade, the one fading out is at zero volume and the one fading in is at full volume. The multipliers go from 1.0 to 0.0 and from 0.0 to 1.0 linearally. Just multiple each sample across the fade by the multiplier for that buffer, and add it to the sample from the other buffer times its multiplier. At each sample, the sum of the two multipliers is 1.0 (full volume).
|
|
|
|
|
I wouldn't worry about it. A 44khz sample rate is trying to handle signals up to 20khz. At 20khz there aren't going to be many samples per complete wave. If you wanted to, you could do a linear interpolation between the two signals for maybe 2-4 samples. That would be plenty of sample and should eliminate any "click".
But then again, I am talking out of my arse.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Well , but what about interpolation ?
How can i implement it ?
And how can i eliminate "clicks" ?
regards
|
|
|
|
|
A click is the sudden change in value for the signal. For example, going from 0 to 65535 for a 16 bit signal.
A linear interpolation example would be:
Last sample for signal a = 20.
First sample for signal b = 220.
To interpolate between the two samples using 4 new samples, you would insert the new samples between the two waves.
60,100,140,180
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|