Hi all!

I'm working in a project in visual c++ 6.0 with the console application and I want to ask you how to create a program that has portability to work in both platforms, windows and linux.

Another thing, which function we use to round a double to the nearest int number?

Thanks,

>how to create a program that has portability to work in both platforms, windows and linux.
Write 100% standard C++, or conditionally compile two different programs customized for the two operating systems.

>which function we use to round a double to the nearest int number?
You'll need to write it yourself, or use a clever workaround. Alternatively, if all you need is to print the rounded double, any formatted output option will do it for you (which opens up ideas for those clever workarounds I was talking about).

Alternatively, if all you need is to print the rounded double, any formatted output option will do it for you (which opens up ideas for those clever workarounds I was talking about).

Eww! That would just be sick.

>Write 100% standard C++, or conditionally compile two different programs customized for the two operating systems.

How do I know what is 100% standard C++?

>You'll need to write it yourself, or use a clever workaround. Alternatively, if all you need is to print the rounded double, any formatted output option will do it for you (which opens up ideas for those clever workarounds I was talking about).

Maybe using a workaround like this:

double dAUX;
int iOUT;
...
if ( (floor(dAUX) - dAUX) < (ceil(dAUX) - dAUX) )
			iOUT = floor(dAUX);
else
			iOUT = ceil(dAUX);

no?

Not bad, but where you say (floor(dAUX) - dAUX) < (ceil(dAUX) - dAUX) , I think you mean (dAUX - floor(dAUX)) < (ceil(dAUX) - dAUX) .

Personally, I prefer floor(dAUX + 0.5) , if I want to always round .5s in the positive direction.

Not bad, but where you say (floor(dAUX) - dAUX) < (ceil(dAUX) - dAUX) , I think you mean (dAUX - floor(dAUX)) < (ceil(dAUX) - dAUX) .

I meant like I had but with this: fabs(floor(dAUX) - dAUX)) < fabs(ceil(dAUX) - dAUX)

Personally, I prefer floor(dAUX + 0.5) , if I want to always round .5s in the positive direction.

Your suggestion is much more simple than mine, I like it, thanks Rashaki Fol and Narue ;)

This article has been dead for over six months. Start a new discussion instead.