次の方法で共有


Math.Max Method (Single, Single)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns the larger of two single-precision floating-point numbers.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function Max ( _
    val1 As Single, _
    val2 As Single _
) As Single
public static float Max(
    float val1,
    float val2
)

Parameters

  • val1
    Type: System.Single
    The first of two single-precision floating-point numbers to compare.
  • val2
    Type: System.Single
    The second of two single-precision floating-point numbers to compare.

Return Value

Type: System.Single
Parameter val1 or val2, whichever is larger. If val1, or val2, or both val1 and val2 are equal to NaN, NaN is returned.

Examples

The following example demonstrates how to use the Max method to return and display the greater of two Single variables.

' This example demonstrates Math.Max()
Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim str As String = "{0}: The greater of {1,3} and {2,3} is {3}."
      Dim nl As String = Environment.NewLine

      Dim xByte1 As Byte = 1
      Dim xByte2 As Byte = 51
      Dim xShort1 As Short = -2
      Dim xShort2 As Short = 52
      Dim xInt1 As Integer = -3
      Dim xInt2 As Integer = 53
      Dim xLong1 As Long = -4
      Dim xLong2 As Long = 54
      Dim xSingle1 As Single = 5.0F
      Dim xSingle2 As Single = 55.0F
      Dim xDouble1 As Double = 6.0
      Dim xDouble2 As Double = 56.0
      Dim xDecimal1 As [Decimal] = 7D
      Dim xDecimal2 As [Decimal] = 57D

      ' The following types are not CLS-compliant.
      Dim xSByte1 As SByte = 101
      Dim xSByte2 As SByte = 111
      Dim xUShort1 As UShort = 102
      Dim xUShort2 As UShort = 112
      Dim xUint1 As UInteger = 103
      Dim xUint2 As UInteger = 113
      Dim xUlong1 As ULong = 104
      Dim xUlong2 As ULong = 114

      outputBlock.Text += String.Format("{0}Display the greater of two values:{0}", nl) & vbCrLf
      outputBlock.Text += String.Format(str, "Byte   ", xByte1, xByte2, Math.Max(xByte1, xByte2)) & vbCrLf
      outputBlock.Text += String.Format(str, "Int16  ", xShort1, xShort2, Math.Max(xShort1, xShort2)) & vbCrLf
      outputBlock.Text += String.Format(str, "Int32  ", xInt1, xInt2, Math.Max(xInt1, xInt2)) & vbCrLf
      outputBlock.Text += String.Format(str, "Int64  ", xLong1, xLong2, Math.Max(xLong1, xLong2)) & vbCrLf
      outputBlock.Text += String.Format(str, "Single ", xSingle1, xSingle2, Math.Max(xSingle1, xSingle2)) & vbCrLf
      outputBlock.Text += String.Format(str, "Double ", xDouble1, xDouble2, Math.Max(xDouble1, xDouble2)) & vbCrLf
      outputBlock.Text += String.Format(str, "Decimal", xDecimal1, xDecimal2, Math.Max(xDecimal1, xDecimal2)) & vbCrLf
      '
      outputBlock.Text += String.Format("{0}(The following types are not CLS-compliant.){0}", nl) & vbCrLf
      outputBlock.Text += String.Format(str, "SByte  ", xSByte1, xSByte2, Math.Max(xSByte1, xSByte2)) & vbCrLf
      outputBlock.Text += String.Format(str, "UInt16 ", xUShort1, xUShort2, Math.Max(xUShort1, xUShort2)) & vbCrLf
      outputBlock.Text += String.Format(str, "UInt32 ", xUint1, xUint2, Math.Max(xUint1, xUint2)) & vbCrLf
      outputBlock.Text += String.Format(str, "UInt64 ", xUlong1, xUlong2, Math.Max(xUlong1, xUlong2)) & vbCrLf
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'Display the greater of two values:
'
'Byte   : The greater of   1 and  51 is 51.
'Int16  : The greater of  -2 and  52 is 52.
'Int32  : The greater of  -3 and  53 is 53.
'Int64  : The greater of  -4 and  54 is 54.
'Single : The greater of   5 and  55 is 55.
'Double : The greater of   6 and  56 is 56.
'Decimal: The greater of   7 and  57 is 57.
'
' (The following types are not CLS-compliant.)
' 
' SByte  : The greater of 101 and 111 is 111.
' UInt16 : The greater of 102 and 112 is 112.
' UInt32 : The greater of 103 and 113 is 113.
' UInt64 : The greater of 104 and 114 is 114.
// This example demonstrates Math.Max()
using System;

class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string str = "{0}: The greater of {1,3} and {2,3} is {3}.";
      string nl = Environment.NewLine;

      byte xByte1 = 1, xByte2 = 51;
      short xShort1 = -2, xShort2 = 52;
      int xInt1 = -3, xInt2 = 53;
      long xLong1 = -4, xLong2 = 54;
      float xSingle1 = 5.0f, xSingle2 = 55.0f;
      double xDouble1 = 6.0, xDouble2 = 56.0;
      Decimal xDecimal1 = 7m, xDecimal2 = 57m;

      // The following types are not CLS-compliant.
      sbyte xSbyte1 = 101, xSbyte2 = 111;
      ushort xUshort1 = 102, xUshort2 = 112;
      uint xUint1 = 103, xUint2 = 113;
      ulong xUlong1 = 104, xUlong2 = 114;

      outputBlock.Text += String.Format("{0}Display the greater of two values:{0}", nl) + "\n";
      outputBlock.Text += String.Format(str, "Byte   ", xByte1, xByte2, Math.Max(xByte1, xByte2)) + "\n";
      outputBlock.Text += String.Format(str, "Int16  ", xShort1, xShort2, Math.Max(xShort1, xShort2)) + "\n";
      outputBlock.Text += String.Format(str, "Int32  ", xInt1, xInt2, Math.Max(xInt1, xInt2)) + "\n";
      outputBlock.Text += String.Format(str, "Int64  ", xLong1, xLong2, Math.Max(xLong1, xLong2)) + "\n";
      outputBlock.Text += String.Format(str, "Single ", xSingle1, xSingle2, Math.Max(xSingle1, xSingle2)) + "\n";
      outputBlock.Text += String.Format(str, "Double ", xDouble1, xDouble2, Math.Max(xDouble1, xDouble2)) + "\n";
      outputBlock.Text += String.Format(str, "Decimal", xDecimal1, xDecimal2, Math.Max(xDecimal1, xDecimal2)) + "\n";
      //
      outputBlock.Text += String.Format("{0}The following types are not CLS-compliant.{0}", nl) + "\n";
      outputBlock.Text += String.Format(str, "SByte  ", xSbyte1, xSbyte2, Math.Max(xSbyte1, xSbyte2)) + "\n";
      outputBlock.Text += String.Format(str, "UInt16 ", xUshort1, xUshort2, Math.Max(xUshort1, xUshort2)) + "\n";
      outputBlock.Text += String.Format(str, "UInt32 ", xUint1, xUint2, Math.Max(xUint1, xUint2)) + "\n";
      outputBlock.Text += String.Format(str, "UInt64 ", xUlong1, xUlong2, Math.Max(xUlong1, xUlong2)) + "\n";
   }
}
/*
This example produces the following results:

Display the greater of two values:

Byte   : The greater of   1 and  51 is 51.
Int16  : The greater of  -2 and  52 is 52.
Int32  : The greater of  -3 and  53 is 53.
Int64  : The greater of  -4 and  54 is 54.
Single : The greater of   5 and  55 is 55.
Double : The greater of   6 and  56 is 56.
Decimal: The greater of   7 and  57 is 57.

(The following types are not CLS-compliant.)

SByte  : The greater of 101 and 111 is 111.
UInt16 : The greater of 102 and 112 is 112.
UInt32 : The greater of 103 and 113 is 113.
UInt64 : The greater of 104 and 114 is 114.
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.