0

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,

3
Contributors
5
Replies
6
Views
11 Years
Discussion Span
Last Post by gampalu
0

>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).

0

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.

0

>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?

0

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.

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.