Bonjour,

J'ai une classe vector générique, j'ai écris une fonction retournant la norme du vector courant, voici le code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
/// <summary>
/// Computes the Euclidien norm of the vector
/// </summary>
/// <param name="dimension"> the dimension of the vector (unused in this function) </param>
/// <returns> returns the norm of the vector </returns>
 
public TField Norm(int dimension)
        {
            var type = typeof(TField);
 
            if (type == typeof(double))
            {
                double sum_square = 0.0;
 
                foreach (TField val in this.vectorObject)
                {
                    var double_value = (double)(object)val;
                    sum_square += (double_value * double_value);
                }
 
                return (TField)(object)(Math.Sqrt(sum_square));
            }
            else if (type == typeof(float))
            {
                float sum_square = 0.0f;
 
                foreach (TField val in this.vectorObject)
                {
                    var float_value = (float)(object)val;
                    sum_square += (float_value * float_value);
                }
                double len = Math.Sqrt((double)sum_square);
                return (TField)(object)(len);
            }
            else
            {
                throw new InvalidOperationException(/*string.Format(InterfaceGraphique.Properties.Resources.MessageGenericArithmeticNotDefined, type.Name)*/);
            }
        }

tout fonctionne quand j'alloue un vector de double, mais quand j'alloue un vector de float et que je lui demande de me retourner sa norme, il me dit qu'il y a un cast invalide à la ligne return (TField)(object)(len); (dans la condition if (type == typeof (float)))

je ne comprends pas pourquoi un float ne peut pas être convertit en object?

Merci

Algernon