Click here to Skip to main content
15,908,775 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: how to use arguments Control in another class Pin
Andrew Brock20-Feb-11 23:50
Andrew Brock20-Feb-11 23:50 
GeneralRe: how to use arguments Control in another class Pin
so0_lanhlung221-Feb-11 1:47
so0_lanhlung221-Feb-11 1:47 
QuestionCFileDialog Pin
john563220-Feb-11 22:16
john563220-Feb-11 22:16 
AnswerRe: CFileDialog Pin
_AnsHUMAN_ 20-Feb-11 22:24
_AnsHUMAN_ 20-Feb-11 22:24 
AnswerRe: CFileDialog Pin
Hans Dietrich20-Feb-11 23:44
mentorHans Dietrich20-Feb-11 23:44 
Questionuse of HitTest method Pin
sarfaraznawaz20-Feb-11 18:39
sarfaraznawaz20-Feb-11 18:39 
AnswerRe: use of HitTest method Pin
Cool_Dev20-Feb-11 19:27
Cool_Dev20-Feb-11 19:27 
GeneralRe: use of HitTest method Pin
sarfaraznawaz20-Feb-11 19:44
sarfaraznawaz20-Feb-11 19:44 
GeneralRe: use of HitTest method Pin
Cool_Dev20-Feb-11 21:22
Cool_Dev20-Feb-11 21:22 
GeneralRe: use of HitTest method Pin
sarfaraznawaz21-Feb-11 0:24
sarfaraznawaz21-Feb-11 0:24 
GeneralRe: use of HitTest method Pin
Cool_Dev21-Feb-11 0:27
Cool_Dev21-Feb-11 0:27 
GeneralRe: use of HitTest method Pin
sarfaraznawaz21-Feb-11 0:53
sarfaraznawaz21-Feb-11 0:53 
GeneralRe: use of HitTest method Pin
Niklas L21-Feb-11 2:07
Niklas L21-Feb-11 2:07 
GeneralRe: use of HitTest method Pin
sarfaraznawaz21-Feb-11 19:13
sarfaraznawaz21-Feb-11 19:13 
QuestionHow to get value 1.3 from a float value 1.333333 Pin
Amrit Agr20-Feb-11 17:21
Amrit Agr20-Feb-11 17:21 
AnswerRe: How to get value 1.3 from a float value 1.333333 Pin
Luc Pattyn20-Feb-11 17:39
sitebuilderLuc Pattyn20-Feb-11 17:39 
AnswerRe: How to get value 1.3 from a float value 1.333333 Pin
Hans Dietrich20-Feb-11 17:51
mentorHans Dietrich20-Feb-11 17:51 
GeneralRe: How to get value 1.3 from a float value 1.333333 Pin
Amrit Agr20-Feb-11 18:44
Amrit Agr20-Feb-11 18:44 
AnswerRe: How to get value 1.3 from a float value 1.333333 Pin
Alain Rist20-Feb-11 20:21
Alain Rist20-Feb-11 20:21 
GeneralRe: How to get value 1.3 from a float value 1.333333 Pin
Andrew Brock20-Feb-11 22:04
Andrew Brock20-Feb-11 22:04 
GeneralRe: How to get value 1.3 from a float value 1.333333 Pin
Alain Rist20-Feb-11 22:43
Alain Rist20-Feb-11 22:43 
AnswerRe: How to get value 1.3 from a float value 1.333333 Pin
Cedric Moonen20-Feb-11 20:48
Cedric Moonen20-Feb-11 20:48 
GeneralRe: How to get value 1.3 from a float value 1.333333 Pin
Alain Rist20-Feb-11 21:12
Alain Rist20-Feb-11 21:12 
GeneralRe: How to get value 1.3 from a float value 1.333333 Pin
Cedric Moonen20-Feb-11 21:21
Cedric Moonen20-Feb-11 21:21 
AnswerRe: How to get value 1.3 from a float value 1.333333 Pin
Andrew Brock20-Feb-11 22:02
Andrew Brock20-Feb-11 22:02 
Some of the suggested answers are ok, but are lacking correct rounding.

Luc Pattyn wrote:
double roundToOneTenth(double number) {
    return 0.1*(int)(10.*number);
}

Should be:
double RoundToOneTenth(double nNumber) {
    return (int)(10.0 * nNumber + 0.5) * 0.1;
}

While this solution works, it requires converting a floating point number to an integer and back to a floating point number. This is somewhat slow.

Hans Dietrich wrote:
double d = floor(1.333333 * 10.) / 10.;

Should be:
double RoundToOneTenth(double nNumber) {
    return floor(nNumber * 10.0 + 0.5) * 0.1;
}

This is the solution I would recommend. Note that I changed /10 to *0.1.
It is faster to multiply than to divide. In a simple case like this the compiler would make this change for you but I am just pointing this out.

Alain Rist wrote:
#include <sstream>
#include <iomanip>
double Round(double val, size_t decimal)
{
    std::ostringstream oss;
    oss << std::fixed << std::setprecision(decimal) << val << std::ends;
    std::istringstream(oss.str()) >> val;
    return val;
}

While this works and saves you from worrying about rounding errors, this is incredibly slow and uses much more memory (although still not much) than other solutions. There is no need for using strings for such a simple operation.
Having said that, it is good that people are posting alternatives to show just how many ways there are for doing this.

Cedric Moonen wrote:
Are you aware that floating points number have a rounding error ? You'll never be able to represent perfectly a float in memory.

While this is absolutely true, it only affects really big numbers or numbers with a high precision.
For instance, 1.0 / 3.0 == 0.333333333333333314829616256247390992939472198486328125 (Wikipedia[^]).
If you are concerned about this, there are macros FLT_EPSILON (for float) and DLB_EPSILON (for double) which define the minimum perfect precision of a single operation.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.