本文整理汇总了C#中System.Single结构体的典型用法代码示例。如果您正苦于以下问题:C# Single结构体的具体用法?C# Single怎么用?C# Single使用的例子?那么恭喜您, 这里精选的结构体代码示例或许可以为您提供帮助。
Single结构体属于System命名空间,在下文中一共展示了Single结构体的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
Single value = .2f;
Single result1 = value * 10f;
Single result2 = 0f;
for (int ctr = 1; ctr <= 10; ctr++)
result2 += value;
Console.WriteLine(".2 * 10: {0:R}", result1);
Console.WriteLine(".2 Added 10 times: {0:R}", result2);
}
}
输出:
.2 * 10: 2 .2 Added 10 times: 2.00000024
示例2: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
Single value = 123.456f;
Single additional = Single.Epsilon * 1e15f;
Console.WriteLine($"{value} + {additional} = {value + additional}");
}
}
输出:
123.456 + 1.401298E-30 = 123.456
示例3: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
Single[] values = { 10.01f, 2.88f, 2.88f, 2.88f, 9.0f };
Single result = 27.65f;
Single total = 0f;
foreach (var value in values)
total += value;
if (total.Equals(result))
Console.WriteLine("The sum of the values equals the total.");
else
Console.WriteLine("The sum of the values ({0}) does not equal the total ({1}).",
total, result);
}
}
输出:
The sum of the values (27.65) does not equal the total (27.65). If the index items in the Console.WriteLine statement are changed to {0:R}, the example displays the following output: The sum of the values (27.6500015) does not equal the total (27.65).
示例4: Main
//引入命名空间
using System;
using System.IO;
public class Example
{
public static void Main()
{
StreamWriter sw = new StreamWriter(@".\Singles.dat");
Single[] values = { 3.2f/1.11f, 1.0f/3f, (float) Math.PI };
for (int ctr = 0; ctr < values.Length; ctr++) {
sw.Write(values[ctr].ToString());
if (ctr != values.Length - 1)
sw.Write("|");
}
sw.Close();
Single[] restoredValues = new Single[values.Length];
StreamReader sr = new StreamReader(@".\Singles.dat");
string temp = sr.ReadToEnd();
string[] tempStrings = temp.Split('|');
for (int ctr = 0; ctr < tempStrings.Length; ctr++)
restoredValues[ctr] = Single.Parse(tempStrings[ctr]);
for (int ctr = 0; ctr < values.Length; ctr++)
Console.WriteLine("{0} {2} {1}", values[ctr],
restoredValues[ctr],
values[ctr].Equals(restoredValues[ctr]) ? "=" : "<>");
}
}
输出:
2.882883 <> 2.882883 0.3333333 <> 0.3333333 3.141593 <> 3.141593
示例5: Main
//引入命名空间
using System;
using System.IO;
public class Example
{
public static void Main()
{
StreamWriter sw = new StreamWriter(@".\Singles.dat");
Single[] values = { 3.2f/1.11f, 1.0f/3f, (float) Math.PI };
for (int ctr = 0; ctr < values.Length; ctr++)
sw.Write("{0:G9}{1}", values[ctr], ctr < values.Length - 1 ? "|" : "" );
sw.Close();
Single[] restoredValues = new Single[values.Length];
StreamReader sr = new StreamReader(@".\Singles.dat");
string temp = sr.ReadToEnd();
string[] tempStrings = temp.Split('|');
for (int ctr = 0; ctr < tempStrings.Length; ctr++)
restoredValues[ctr] = Single.Parse(tempStrings[ctr]);
for (int ctr = 0; ctr < values.Length; ctr++)
Console.WriteLine("{0} {2} {1}", values[ctr],
restoredValues[ctr],
values[ctr].Equals(restoredValues[ctr]) ? "=" : "<>");
}
}
输出:
2.882883 = 2.882883 0.3333333 = 0.3333333 3.141593 = 3.141593
示例6: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
Double value1 = 1/3.0;
Single sValue2 = 1/3.0f;
Double value2 = (Double) sValue2;
Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2,
value1.Equals(value2));
}
}
输出:
0.33333333333333331 = 0.3333333432674408: False
示例7: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
float value1 = .3333333f;
float value2 = 1.0f/3;
Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2));
}
}
输出:
0.3333333 = 0.333333343: False
示例8: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
float value1 = 10.201438f;
value1 = (float) Math.Sqrt((float) Math.Pow(value1, 2));
float value2 = (float) Math.Pow((float) value1 * 3.51f, 2);
value2 = ((float) Math.Sqrt(value2)) / 3.51f;
Console.WriteLine("{0} = {1}: {2}\n",
value1, value2, value1.Equals(value2));
Console.WriteLine("{0:G9} = {1:G9}", value1, value2);
}
}
输出:
10.20144 = 10.20144: False 10.201438 = 10.2014389
示例9: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
float value1 = .3333333f;
float value2 = 1.0f/3;
int precision = 7;
value1 = (float) Math.Round(value1, precision);
value2 = (float) Math.Round(value2, precision);
Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2));
}
}
输出:
0.3333333 = 0.3333333: True
示例10: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
float one1 = .1f * 10;
float one2 = 0f;
for (int ctr = 1; ctr <= 10; ctr++)
one2 += .1f;
Console.WriteLine("{0:R} = {1:R}: {2}", one1, one2, one1.Equals(one2));
Console.WriteLine("{0:R} is approximately equal to {1:R}: {2}",
one1, one2,
IsApproximatelyEqual(one1, one2, .000001f));
}
static bool IsApproximatelyEqual(float value1, float value2, float epsilon)
{
// If they are equal anyway, just return True.
if (value1.Equals(value2))
return true;
// Handle NaN, Infinity.
if (Double.IsInfinity(value1) | Double.IsNaN(value1))
return value1.Equals(value2);
else if (Double.IsInfinity(value2) | Double.IsNaN(value2))
return value1.Equals(value2);
// Handle zero to avoid division by zero
double divisor = Math.Max(value1, value2);
if (divisor.Equals(0))
divisor = Math.Min(value1, value2);
return Math.Abs(value1 - value2)/divisor <= epsilon;
}
}
输出:
1 = 1.00000012: False 1 is approximately equal to 1.00000012: True
示例11: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
float value1 = 1.163287e-36f;
float value2 = 9.164234e-25f;
float result = value1 * value2;
Console.WriteLine("{0} * {1} = {2}", value1, value2, result);
Console.WriteLine("{0} = 0: {1}", result, result.Equals(0.0f));
}
}
输出:
1.163287E-36 * 9.164234E-25 = 0 0 = 0: True
示例12: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
float value1 = 3.065e35f;
float value2 = 6.9375e32f;
float result = value1 * value2;
Console.WriteLine("PositiveInfinity: {0}",
Single.IsPositiveInfinity(result));
Console.WriteLine("NegativeInfinity: {0}\n",
Single.IsNegativeInfinity(result));
value1 = -value1;
result = value1 * value2;
Console.WriteLine("PositiveInfinity: {0}",
Single.IsPositiveInfinity(result));
Console.WriteLine("NegativeInfinity: {0}",
Single.IsNegativeInfinity(result));
}
}
输出:
PositiveInfinity: True NegativeInfinity: False PositiveInfinity: False NegativeInfinity: True
示例13: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
dynamic[] values = { Byte.MinValue, Byte.MaxValue, Decimal.MinValue,
Decimal.MaxValue, Double.MinValue, Double.MaxValue,
Int16.MinValue, Int16.MaxValue, Int32.MinValue,
Int32.MaxValue, Int64.MinValue, Int64.MaxValue,
SByte.MinValue, SByte.MaxValue, UInt16.MinValue,
UInt16.MaxValue, UInt32.MinValue, UInt32.MaxValue,
UInt64.MinValue, UInt64.MaxValue };
float sngValue;
foreach (var value in values) {
if (value.GetType() == typeof(Decimal) ||
value.GetType() == typeof(Double))
sngValue = (float) value;
else
sngValue = value;
Console.WriteLine("{0} ({1}) --> {2:R} ({3})",
value, value.GetType().Name,
sngValue, sngValue.GetType().Name);
}
}
}
输出:
0 (Byte) --> 0 (Single) 255 (Byte) --> 255 (Single) -79228162514264337593543950335 (Decimal) --> -7.92281625E+28 (Single) 79228162514264337593543950335 (Decimal) --> 7.92281625E+28 (Single) -1.79769313486232E+308 (Double) --> -Infinity (Single) 1.79769313486232E+308 (Double) --> Infinity (Single) -32768 (Int16) --> -32768 (Single) 32767 (Int16) --> 32767 (Single) -2147483648 (Int32) --> -2.14748365E+09 (Single) 2147483647 (Int32) --> 2.14748365E+09 (Single) -9223372036854775808 (Int64) --> -9.223372E+18 (Single) 9223372036854775807 (Int64) --> 9.223372E+18 (Single) -128 (SByte) --> -128 (Single) 127 (SByte) --> 127 (Single) 0 (UInt16) --> 0 (Single) 65535 (UInt16) --> 65535 (Single) 0 (UInt32) --> 0 (Single) 4294967295 (UInt32) --> 4.2949673E+09 (Single) 0 (UInt64) --> 0 (Single) 18446744073709551615 (UInt64) --> 1.84467441E+19 (Single)
示例14: Main
//引入命名空间
using System;
public class Example
{
public static void Main()
{
float[] values = { Single.MinValue, -67890.1234f, -12345.6789f,
12345.6789f, 67890.1234f, Single.MaxValue,
Single.NaN, Single.PositiveInfinity,
Single.NegativeInfinity };
checked {
foreach (var value in values) {
try {
Int64 lValue = (long) value;
Console.WriteLine("{0} ({1}) --> {2} (0x{2:X16}) ({3})",
value, value.GetType().Name,
lValue, lValue.GetType().Name);
}
catch (OverflowException) {
Console.WriteLine("Unable to convert {0} to Int64.", value);
}
try {
UInt64 ulValue = (ulong) value;
Console.WriteLine("{0} ({1}) --> {2} (0x{2:X16}) ({3})",
value, value.GetType().Name,
ulValue, ulValue.GetType().Name);
}
catch (OverflowException) {
Console.WriteLine("Unable to convert {0} to UInt64.", value);
}
try {
Decimal dValue = (decimal) value;
Console.WriteLine("{0} ({1}) --> {2} ({3})",
value, value.GetType().Name,
dValue, dValue.GetType().Name);
}
catch (OverflowException) {
Console.WriteLine("Unable to convert {0} to Decimal.", value);
}
Double dblValue = value;
Console.WriteLine("{0} ({1}) --> {2} ({3})",
value, value.GetType().Name,
dblValue, dblValue.GetType().Name);
Console.WriteLine();
}
}
}
}
// The example displays the following output for conversions performed
// in a checked context:
// Unable to convert -3.402823E+38 to Int64.
// Unable to convert -3.402823E+38 to UInt64.
// Unable to convert -3.402823E+38 to Decimal.
// -3.402823E+38 (Single) --> -3.40282346638529E+38 (Double)
//
// -67890.13 (Single) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
// Unable to convert -67890.13 to UInt64.
// -67890.13 (Single) --> -67890.12 (Decimal)
// -67890.13 (Single) --> -67890.125 (Double)
//
// -12345.68 (Single) --> -12345 (0xFFFFFFFFFFFFCFC7) (Int64)
// Unable to convert -12345.68 to UInt64.
// -12345.68 (Single) --> -12345.68 (Decimal)
// -12345.68 (Single) --> -12345.6787109375 (Double)
//
// 12345.68 (Single) --> 12345 (0x0000000000003039) (Int64)
// 12345.68 (Single) --> 12345 (0x0000000000003039) (UInt64)
// 12345.68 (Single) --> 12345.68 (Decimal)
// 12345.68 (Single) --> 12345.6787109375 (Double)
//
// 67890.13 (Single) --> 67890 (0x0000000000010932) (Int64)
// 67890.13 (Single) --> 67890 (0x0000000000010932) (UInt64)
// 67890.13 (Single) --> 67890.12 (Decimal)
// 67890.13 (Single) --> 67890.125 (Double)
//
// Unable to convert 3.402823E+38 to Int64.
// Unable to convert 3.402823E+38 to UInt64.
// Unable to convert 3.402823E+38 to Decimal.
// 3.402823E+38 (Single) --> 3.40282346638529E+38 (Double)
//
// Unable to convert NaN to Int64.
// Unable to convert NaN to UInt64.
// Unable to convert NaN to Decimal.
// NaN (Single) --> NaN (Double)
//
// Unable to convert Infinity to Int64.
// Unable to convert Infinity to UInt64.
// Unable to convert Infinity to Decimal.
// Infinity (Single) --> Infinity (Double)
//
// Unable to convert -Infinity to Int64.
// Unable to convert -Infinity to UInt64.
// Unable to convert -Infinity to Decimal.
// -Infinity (Single) --> -Infinity (Double)
// The example displays the following output for conversions performed
// in an unchecked context:
// -3.402823E+38 (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
// -3.402823E+38 (Single) --> 9223372036854775808 (0x8000000000000000) (UInt64)
// Unable to convert -3.402823E+38 to Decimal.
// -3.402823E+38 (Single) --> -3.40282346638529E+38 (Double)
//
// -67890.13 (Single) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
// -67890.13 (Single) --> 18446744073709483726 (0xFFFFFFFFFFFEF6CE) (UInt64)
// -67890.13 (Single) --> -67890.12 (Decimal)
// -67890.13 (Single) --> -67890.125 (Double)
//
// -12345.68 (Single) --> -12345 (0xFFFFFFFFFFFFCFC7) (Int64)
// -12345.68 (Single) --> 18446744073709539271 (0xFFFFFFFFFFFFCFC7) (UInt64)
// -12345.68 (Single) --> -12345.68 (Decimal)
// -12345.68 (Single) --> -12345.6787109375 (Double)
//
// 12345.68 (Single) --> 12345 (0x0000000000003039) (Int64)
// 12345.68 (Single) --> 12345 (0x0000000000003039) (UInt64)
// 12345.68 (Single) --> 12345.68 (Decimal)
// 12345.68 (Single) --> 12345.6787109375 (Double)
//
// 67890.13 (Single) --> 67890 (0x0000000000010932) (Int64)
// 67890.13 (Single) --> 67890 (0x0000000000010932) (UInt64)
// 67890.13 (Single) --> 67890.12 (Decimal)
// 67890.13 (Single) --> 67890.125 (Double)
//
// 3.402823E+38 (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
// 3.402823E+38 (Single) --> 0 (0x0000000000000000) (UInt64)
// Unable to convert 3.402823E+38 to Decimal.
// 3.402823E+38 (Single) --> 3.40282346638529E+38 (Double)
//
// NaN (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
// NaN (Single) --> 0 (0x0000000000000000) (UInt64)
// Unable to convert NaN to Decimal.
// NaN (Single) --> NaN (Double)
//
// Infinity (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
// Infinity (Single) --> 0 (0x0000000000000000) (UInt64)
// Unable to convert Infinity to Decimal.
// Infinity (Single) --> Infinity (Double)
//
// -Infinity (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
// -Infinity (Single) --> 9223372036854775808 (0x8000000000000000) (UInt64)
// Unable to convert -Infinity to Decimal.
// -Infinity (Single) --> -Infinity (Double)