If I use operator++, it will increase by one, but is it possible to increase by 0.1?

Asked 2 years ago, Updated 2 years ago, 37 views

As the title suggests.

public float speed=1.0f;

in void update to

transform.position+=transform.forward*speed;

It's always moving forward, so for example,

 if (Input.GetKey(KeyCode.UpArrow))
    {
        speed++;
    }

If you write like this, the speed increases by one every time you press a key, but I want to reduce the number that increases.
Is it possible to increase by 0.1?

c#

2022-09-30 19:24

3 Answers

speed+=0.1f;

You can specify any value that increases by describing .The expected increments are program-specific, so having multiple meanings for the ++ operator is confusing and should be explicitly stated.

Although suzukis has commented that overload is possible and shimitei has responded, we do not recommend using overload for this purpose.This is because you need to have a large number of members to define the numerical type.Mr. pgrho mentioned a few members, but there is also a shortage of four arithmetic operations and comparative arithmetic operations, so it should be quite difficult to use them as numerical values.

I wrote it for your reference, but I'm not sure if it's all. ++ I recommend speed+=0.1f as I mentioned earlier.
(If you have an implicit type conversion from float to Speed, I thought Speed binomial operators would be enough, so I omitted Speed and float operators.)

structure Speed:IEquatable<Speed>,IEquatable<float>,ICompatible,ICompatible<Speed>,ICompatible<float>,IFormatable,IConvertible{
    public float value {get;set;}

    public Speed (float data) = > Value = data;
    public static implicit operator Speed (floatf) = > new Speed(f);
    public static implicit operator float (Speed f) = > f. Value;

    public static speed operator+(Speeds) =>s;
    public static Speed operator -(Speeds) =>-s.Value;
    public static Speed operator++(Speeds) =>s +0.1f;
    public static speed operator --(Speeds) =>s-0.1f;

    public static speed operator+(Speed1, Speed2) =>s1.Value+s2.Value;
    public static speed operator -(Speed1, Speed2) =>s1.Value-s2.Value;
    public static speed operator*(Speed1, Speed2) =>s1.Value*s2.Value;
    public static speed operator/(Speed1, Speed2) =>s1.Value/s2.Value;
    public static Speed operator%(Speed1,Speed2) =>s1.Value%s2.Value;

    public static bool operator==(Speed 1, Speed 2)=>s1.Value==s2.Value;
    public static bool operator!=(Speed 1, Speed 2)=>s1.Value!=s2.Value;
    public static bool operator<(Speed 1, Speed 2) =>s1.Value<s2.Value;
    public static bool operator > (Speed 1, Speed 2) = > s1.Value > s2.Value;
    public static bool operator<=(Speed 1, Speed 2)=>s1.Value<=s2.Value;
    public static bool operator>=(Speed 1, Speed 2)=>s1.Value>=s2.Value;

    public override int GetHashCode() = > Value.GetHashCode();

    public override bool Equals (object obj) = > obj is Speed? Equals(s): objis float f? Equals(f): false;
    public bool Equals (Speed other) = > Equals (other.Value);
    public bool Equals (float other) = > Value.Equals (other);

    public int CompareTo(object obj) = > obj is Speed? CompareTo(s): objis float f?CompareTo(f):throw new ArgumentException();
    public int CompareTo (Speed other) = > CompareTo (other.Value);
    public int CompareTo(float other)=>Value.CompareTo(other);

    public override string ToString()=>Value.ToString();
    public string ToString (string format) = > Value.ToString (format);
    public string ToString (IFormatProvider formatProvider) = > Value.ToString (formatProvider);
    public string ToString (string format, IFformatProvider formatProvider) = > Value.ToString (format, formatProvider);

    publicTypeCodeGetTypeCode()=>Value.GetTypeCode();
    booolIconvertible.ToBoolean(IFormatProvider provider)=>((IConvertible)Value).ToBoolean(provider);
    charIconvertible.ToChar(IFormatProvider provider)=>(IConvertible)Value).ToChar(provider);
    sbyte Iconvertible.ToSByte (IFormatProvider provider) = > ((IConvertible) Value).ToSByte (provider);
    byte Iconvertible.ToByte (IFormatProvider provider) = > ((IConvertible) Value).ToByte (provider);
    shortIconvertible.ToInt16(IFormatProvider provider)=>(IConvertible)Value).ToInt16(provider);
    US>ushortIconvertible.ToUInt16(IFormatProvider provider)=>((IConvertible)Value).ToUInt16(provider);
    int IConvertible.ToInt32(IFormatProvider provider)=>(IConvertible) Value).ToInt32(provider);
    uintIConvertible.ToUInt32(IFormatProvider provider)=>((IConvertible)Value).ToUInt32(provider);
    longIconvertible.ToInt64(IFormatProvider provider)=>(IConvertible)Value).ToInt64(provider);
    ulongIConvertible.ToUInt64(IFormatProvider provider)=>((IConvertible)Value).ToUInt64(provider);
    floatIconvertible.ToSingle(IFormatProvider provider)=>(IConvertible)Value).ToSingle(provider);
    doubleIconvertible.ToDouble(IFormatProvider provider)=>(IConvertible)Value).ToDouble(provider);
    decimalIconvertible.ToDecimal(IFormatProvider provider)=>((IConvertible)Value).ToDecimal(provider);
    DateTimeIConvertible.ToDateTime(IFormatProvider provider)=>((IConvertible)Value).ToDateTime(provider);
    objectIconvertible.ToType(Type conversionType,IFormatProvider provider)=>((IConvertible)Value).ToType(conversionType,provider);
}


2022-09-30 19:24

The operator++ is defined in the programming language C# and "an operator that increases the value of an integer variable by one", so it cannot be any other function.

Use the operator + = and set the increment to less than 1.

For example,

public float speed=1.0f;

void changeSpeed(changeValue) {
   speed+=changeValue;
}

If you define the class Robot like this,

Robotr=new Robot();

r.change (0.1); // Increase robot speed by 0.1 (accelerate)
r.change (-0.05); // Reduce robot speed by 0.05 (decelerate)

You can accelerate and decelerate depending on the value of the argument.


2022-09-30 19:24

You can also create a new class so that overloading the ++ operator increases by 0.1

.
class program
{
    static void Main (string[]args)
    {
        Speed speed = 1.0f;
        Console.WriteLine(speed);//1
        speed++;
        Console.WriteLine(speed); //1.1
        float forward = 30.0f;
        Console.WriteLine (forward* speed); //33
        float position = 100.0f;
        position + = forward * speed;
        Console.WriteLine(position); //133
    }
}

class speed
{
    public float value {get;set;}
    public Speed (float data) {this.Value=data;}
    // Implicitly convertible from float.Speed speed = 1.0f;
    public static implicit operator Speed (float f)
    {
        return new Speed (f);
    }
    // Implicitly convertible to float.float pos = new Speed (1.0f);
    public static implicit operator float (Speed f)
    {
        return f.Value;
    }
    // ++ operator overload, increasing by 0.1.
    public static Speed operator++ (Speed f)
    {
        Speed tmp = new Speed (f. Value + 0.1f);
        return tmp;
    }
    // * Operator overload
    public static speed operator* (Speeds, float f)
    {
        return new Speed(s.Value*f);
    }
    public static speed operator* (float f, speed)
    {
        return new Speed(s.Value*f);
    }
    // + Overload operator
    public static speed operator+ (Speeds, float f)
    {
        return new Speed(s.Value+f);
    }
    public static speed operator+ (float f, speed)
    {
        return new Speed(s.Value+f);
    }
    public override string ToString() {return this.Value.ToString();}
}


2022-09-30 19:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.