# Extract the integer part from a real valued variable in AceGen

I would like to extract the integer part of a real variable. To be more specific, given a vector alpha I would like to extract the integer part of (||alpha||+Pi)/(2*Pi):

Get["AceGen"];

SMSInitialize["GetFloor", "Language" -> "C"];
SMSModule["GetFloor", Real[alpha$$[3], floor$$]];
α ⊨ Table[SMSReal[alpha$$[i]], {i, 1, 3}]; GetFloor[α_] := Module[ {normα}, normα ⊨ SMSSqrt[α . α]; SMSInteger[IntegerPart[N[normα + Pi/(2*Pi)]]] ]; SMSExport[GetFloor[α], floor$$];
SMSWrite[];

FilePrint["GetFloor.c"];


I checked in the C code produced and the instruction IntegerPart seems skipped by Acegen.

Is what I am trying to do possible at all in AceGen ? If so, what would be the syntax ?

• Perhaps the documentation for AceGen could help? – Somos Jan 5 '19 at 21:52
• The IntegerPart is a Mathematica function and will be evaluated at the time when you call your Module. It cannot be transfered to C code. If you define a variable as an integer i.e. n[DoubleRightTee]SMSInteger[...], the integer part of a real value will be taken and should be visible in your generated C code as iXXX=(int)V[...]. Also n has to be defined in both branches of SMSIF by using the appropriate equal AceGen signs (se help for SMSIf) or if you use inline IF: n[DoubleRightTee]SMSIF[...]; – BHudobivnik Jan 6 '19 at 21:16
• @MarcoMag Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Pinti Jan 11 '19 at 9:53
• @MarcoMag I have edited the code in your question for readability. I hope it still conveys your intention? I think your AceGen syntax is not correct, but maybe there are other ways to achieve your goals. I suggest that you expand your question with a more complete minimal working example and some additional explanation. – Pinti Jan 11 '19 at 9:59
• @Pinti thanks for the suggestion. I modified the question. Regarding the formatting of the code, I followed the instructions given here but without success – MarcoMag Jan 11 '19 at 15:17

The function SMSFloor[] seems to do the job. Here is the code:

Get["AceGen"];

SMSInitialize["GetFloor", "Language" -> "C", "Mode" -> "Debug"];
SMSModule["GetFloor", Real[alpha$$[3], floor$$]];

α ⊨ Table[SMSReal[alpha$$[i]], {i, 1, 3}]; GetFloor[α_] := Module[ {normα}, normα ⊨ SMSSqrt[α.α]; SMSFloor[normα/(2*Pi) + 1/2] ] SMSExport[GetFloor[α], floor$$];
SMSWrite[];

FilePrint["GetFloor.c"];

• Are you sure that this code really works? Because FilePrint shows suspiciously short C subroutine. I think AceGen doesn't play nicely with local symbols in Module. (?) – Pinti Jan 11 '19 at 16:44
• @Pinti: My bad, I did not test the C code produced. The answer that was suggested lead to incorrect result despite the fact that AceGen did not throw any error. The value return was always (*floor)=floor(0.13894156133957242e1);. However, putting normα outside N[] seems to solve the problem. – MarcoMag Jan 27 '19 at 17:27
• Good job! In fact you can omit N function at all, because AceGen knows it has to convert it to real number. – Pinti Jan 28 '19 at 9:58