|
How can I make a trunc() function in C++,
Example:
trunc(7.1) = 7;
trunc(7.6) = 8;
|
|
|
|
|
What function? Just cast it to an integer. Or if you want a function:
__inline int trunc(float f)
{
return (int)f;
}
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
If I do it in this way, the results are:
trunc(7.1) = 7;
trunc(7.6) = 7;
|
|
|
|
|
Oh sorry, I overlooked your previous post. You can use one of those flooring or ceiling functions.
[Added] OK, Stuart just replied with a floor function at the same time. [/added]
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Presuming you want similar behaviour for numbers < 0
trunc(-7.1) = -7;
trunc(-7.6) = -8;
then this should work
int trunc(double d) { return floor(d+0.5); }
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
rounded_num = (int)(num + (num < 0 ? -0.5 : 0.5));
|
|
|
|
|
You call it trunc() , but that is the expected behaviour of round() function.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
I was guessing that the OP is trying to implement the functionality himself.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Your guess is right, I suppose. However, OP must not give, even to his functions, deceiving names, unless, of course, he is a Klingon programmer.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
you are looking for a rounding function
int round(double d)
{
return (int) (d + 0.5);
}
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
The FPU already does this for you.
inline int iround(double v){
int retval;
__asm fld qword ptr [v]
__asm fistp dword ptr [retval]
return retval;
}
inline unsigned int uround(double v){
unsigned retval;
__asm fld qword ptr [v]
__asm fistp dword ptr [retval]
return retval;
}
|
|
|
|
|
|
Hiii,,,
The following code doesn't take .
How to make it correct?
UINT64 ui_offset;
LONGLONG ll_array[ ui_offset];
Showing constraint error....!!!
Thank u
|
|
|
|
|
ui_offset has to be a constant.
Try this -
const UINT64 ui_offset = <value>;
LONGLONG ll_array[ui_offset];
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Hiiii....
I tried with
const UINT64 ui_offset = 1000000;
LONGLONG ll_array[ui_offset];
It works properly...
Thanking you......
|
|
|
|
|
What is woring in my following codes run time error is occuring for both code
int main()
{LPTSTR strResult;
LPTSTR strDefault=L"no";
DWORD d=255;
GetPrivateProfileString(L"data",L"Patchcab ",
strDefault,strResult,d,
L"D:\\TMP\\patchpolicy.ini");
printf("%s",strResult);
}
int main()
{
LPTSTR strResult=L"";
LPTSTR strDefault=L"no";
DWORD d=255;
GetPrivateProfileString(L"data",L"Patchcab ",strDefault,strResult
,d,L"D:\\TMP\\patchpolicy.ini");
printf("%s",strResult);
}
|
|
|
|
|
There is no memory allocated for the LpReturnedString parameter.
You're declaring it as LPTSTR strResult which is only a pointer.
You will need to do this -
TCHAR strResult[255];
DWORD d = 255;
Also change printf to _tprintf .
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
|
Hey, I think this is just a little mistake somewhere. I am new to using the STL algorithms, so i am totally botching it somehow.
My code looks like this:
typedef std::string Node;
typedef std::vector<Node> Cluster;
and down in the code i want to do this:
bool contains (const Cluster& cl, const Node& n)
{
Cluster::iterator it = find (cl.begin (), cl.end (), n);
(it == cl.end ()) ? return false : return true;
}
is that possible? or am i not allowed to do that? Thanks in advance!
|
|
|
|
|
Looks good except for the last return.
It should be return (it == cl.end ()) ? false : true;
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
«_Superman_» wrote: It should be return (it == cl.end ()) ? false : true;
return (it != cl.end());
FFY from me & Jijo.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
«_Superman_»
I love work. It gives me something to do between weekends.
|
|
|
|
|
Howdy'
I have some very bad code that should crash but I'm having difficulty figuring out why it does not crash (or even give out warnings); it works in VS2003 in release&debug and in VS2008 in debug, not in release.
class Info
{
public:
Info();
Info(std::wstring s1, std::wstring s2);
std::wstring m_string2;
};
struct myStruct
{
std::vector <Info*> m_JobInfoVector;
};
m_MyStruct = (myStruct*)malloc(sizeof (myStruct) );
ZeroMemory(m_MyStruct, sizeof(myStruct));
Info* p = new Info( std::wstring(_T("allo")) );
m_MyStruct->m_JobInfoVector.clear();
Question : What happens to the vector member when the ZeroMemory is called ? and why it does not crash in debug but it does in release ?
Thanks.
Max.
This signature was proudly tested on animals.
|
|
|
|
|
Why worry? The code is erronous, so the result of executing it is undefined. Just replace your malloc with new myStruct ...and don't, just don't ever write over a class with something like ZeroMemory - it just doesn't make sense. You have constructors to initialise structs and classes in C++.
But anyway - the error is in comparison of iterators (which clear does) - in VS2008, Debug mode iterators are different to Release mode iterators unless you specifically enable iterator debugging, which probably has something to do with it.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
I know that, but I was wondering why it did not crash; anyway, the code has been fixed.
Thanks.
This signature was proudly tested on animals.
|
|
|
|