[Mono-dev] Patch for Math.cs
Alan McGovern
alan.mcgovern at gmail.com
Sun Mar 4 19:02:40 EST 2007
Just out of interest, could you check the performance of the method if you
change code patterns like:
if (value > 0)
return Floor (value + 0.5);
else
return Ceiling (value - 0.5);
to:
return (value > 0) ? Floor(value+0.5) : Ceiling(value - 0.5);
When i was profiling stuff before i think there was a performance boost when
using the turnary form, I may be wrong though. If it does turn out to be
faster, you could make that change to. If it isn't, no worries.
Alan.
On 3/4/07, Dennis Hayes <denisraytek at yahoo.com> wrote:
>
> This is a minor clean up of Math.cs.
> Basicly, in the methods that use MidpointRounding Mode, it moves the check
> for a valid mode to the end of the method.
>
> Since the mode is checked in the method, doing this eliminates the extra
> check. Also, the check should never fail, since an enum is being passed in.
> This will make the code a little faster.
>
> I don't have svn access setup, so I need someone to check this in also.
>
> I did not include any tests, because this should not have added or
> modifyed anything testable.
>
> Should I do anything elseor do anything differently?
> Would this be better as a switch?
> Thanks,
> Dennis
>
> ------------------------------
> Need a quick answer? Get one in minutes from people who know. Ask your
> question on Yahoo! Answers<http://answers.yahoo.com/;_ylc=X3oDMTFvbGNhMGE3BF9TAzM5NjU0NTEwOARfcwMzOTY1NDUxMDMEc2VjA21haWxfdGFnbGluZQRzbGsDbWFpbF90YWcx>.
> --0-164504892-1173052624=:24413--
>
>
> Index: Math.cs
> ===================================================================
> --- Math.cs (revision 73696)
> +++ Math.cs (working copy)
> @@ -373,27 +373,27 @@
> [MonoTODO ("Not implemented")]
> public static decimal Round (decimal d, MidpointRounding
> mode)
> {
> - if ((mode != MidpointRounding.ToEven) && (mode !=
> MidpointRounding.AwayFromZero))
> - throw new ArgumentException ("The value '"
> + mode + "' is not valid for this usage of the type MidpointRounding.",
> "mode");
> -
> if (mode == MidpointRounding.ToEven)
> return Round (d);
> - throw new NotImplementedException ();
> + if (mode == MidpointRounding.AwayFromZero)
> + throw new NotImplementedException ();
> +
> + throw new ArgumentException ("The value '" + mode
> + "' is not valid for this usage of the type MidpointRounding.", "mode");
> }
>
> [MonoTODO ("Not implemented")]
> public static decimal Round (decimal d, int decimals,
> MidpointRounding mode)
> {
> - if ((mode != MidpointRounding.ToEven) && (mode !=
> MidpointRounding.AwayFromZero))
> - throw new ArgumentException ("The value '"
> + mode + "' is not valid for this usage of the type MidpointRounding.",
> "mode");
> -
> if (mode == MidpointRounding.ToEven)
> return Round (d, decimals);
> - throw new NotImplementedException ();
> + if (mode == MidpointRounding.AwayFromZero)
> + throw new NotImplementedException ();
> +
> + throw new ArgumentException ("The value '" + mode
> + "' is not valid for this usage of the type MidpointRounding.", "mode");
> }
> #endif
>
> - [MethodImplAttribute (MethodImplOptions.InternalCall)]
> + [MethodImplAttribute (MethodImplOptions.InternalCall)]
> public extern static double Round (double d);
>
> public static double Round (double value, int digits)
> @@ -411,26 +411,25 @@
> #if NET_2_0
> public static double Round (double value, MidpointRounding
> mode)
> {
> - if ((mode != MidpointRounding.ToEven) && (mode !=
> MidpointRounding.AwayFromZero))
> - throw new ArgumentException ("The value '"
> + mode + "' is not valid for this usage of the type MidpointRounding.",
> "mode");
> -
> if (mode == MidpointRounding.ToEven)
> return Round (value);
> if (value > 0)
> return Floor (value + 0.5);
> else
> return Ceiling (value - 0.5);
> +
> + throw new ArgumentException ("The value '" + mode
> + "' is not valid for this usage of the type MidpointRounding.", "mode");
> }
>
> [MonoTODO ("Not implemented")]
> public static double Round (double value, int digits,
> MidpointRounding mode)
> {
> - if ((mode != MidpointRounding.ToEven) && (mode !=
> MidpointRounding.AwayFromZero))
> - throw new ArgumentException ("The value '"
> + mode + "' is not valid for this usage of the type MidpointRounding.",
> "mode");
> -
> - if (mode == MidpointRounding.ToEven)
> + if (mode == MidpointRounding.ToEven)
> return Round (value, digits);
> - throw new NotImplementedException ();
> + if (mode == MidpointRounding.ToEven)
> + throw new NotImplementedException ();
> +
> + throw new ArgumentException ("The value '" + mode
> + "' is not valid for this usage of the type MidpointRounding.", "mode");
> }
>
> public static double Truncate (double d)
> @@ -454,7 +453,7 @@
> }
> #endif
>
> - public static int Sign (decimal value)
> + public static int Sign (decimal value)
> {
> if (value > 0) return 1;
> return (value == 0)? 0: -1;
>
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20070305/292dd116/attachment.html
More information about the Mono-devel-list
mailing list