math class .net framework library

Started by
6 comments, last by makingroulette 19 years, 2 months ago
I am trying to use the Math class to perform rounding of numbers. The Math class contains various rounding functions. Math::Round(3.44, 1); //Returns 3.4. Math::Round(3.45, 1); //Returns 3.4. Math::Round(3.46, 1); //Returns 3.5. The problem is that I don't know what header file I have to include, if any at all. just using the Math namespace specifier doesn;t seem to be enough. I don;t have any experience with the .NET framework library. Hope you can help
Advertisement
Since you mentioned header files I'm assuming that you're using managed C++. You don't need header files just a reference to mscorlib.dll (#using <mscorlib.dll>) and namespace references to the math library:

#using <mscorlib.dll>int main(){   System::Console::WriteLine( System::Math::Round( 3.44, 1 ).ToString() );   System::Console::WriteLine( System::Math::Round( 3.45, 1 ).ToString() );   System::Console::WriteLine( System::Math::Round( 3.46, 1 ).ToString() );   return 0;}
---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }
ive just tried that and i didn;t work.

c:\Projects\PickingConsoleApplication\main.cpp(3) : fatal error C1190: managed targeted code requires '#using <mscorlib.dll>' and '/clr' option

and got this message.

im not sure if im using managed C++

im creating an empty console application
you have to use managed c++ to access .net, unless you are using C#, etc.

throw the /clr switch in the compiler.
make sure System.dll is referenced in the project.
I put the above code in a file called c:\test\test.cpp

In a DOS window (Start->Run->cmd.exe->OK) I cd to the directory where test.cpp is and initialize the VS 2003 environment:

cd c:\test
call "%VS71COMNTOOLS%vsvars32.bat"


I then compile the program:

C:\test> cl /clr test.cpp

and run it:

C:\test> test.exe
3.4
3.4
3.5

bold is what I typed.
---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }
makingroulette, you can also add the /clr switch from within Visual Studio:

1. Right-click on your project in Solution Explorer (it should be contained within a 'Solution').
2. Click Properties.
3. Make sure General is selected on the left.
4. Set "Use Managed Extensions" to yes.

Thanks for your help i got it all working.

This topic is closed to new replies.

Advertisement