Précédent   Forum des professionnels en informatique > Bases de données > MS SQL-Server > Développement
Développement Forum d'entraide sur le Transact-SQL, le CLR, les procédures stockées, les triggers, les requêtes SQL
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 16/09/2008, 16h36   #1
Expert Confirmé
 
Avatar de franculo_caoulene
 
Inscription : octobre 2003
Messages : 2 886
Détails du profil
Informations forums :
Inscription : octobre 2003
Messages : 2 886
Points : 2 559
Points : 2 559
Par défaut ACOS() provoque "Une erreur de domaine s'est produite"

Salut,

J'ai un comportement étrange qui, je pense, est lié au type des données. Je souhaite calculer arccos(1).
Si 1 est un entier acos(1) me retourne 0. Par contre, si 1 vient d'un calcul de flottants la requête retourne l'erreur :
Citation:
Une erreur de domaine s'est produite
Voilà de quoi illustrer le problème :
Code :
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
41
42
43
44
45
46
47
CREATE TABLE [dbo].[VILLE_1](
	[LIB_VILLE] [nvarchar](255) NULL,
	[CODE_INSEE] [float] NOT NULL,
	[LATITUDE] [nvarchar](255) NULL,
	[LONGITUDE] [nvarchar](255) NULL
) ON [PRIMARY]
 
INSERT INTO [dbo].[VILLE_1]([LIB_VILLE], [CODE_INSEE], [LATITUDE], [LONGITUDE]) VALUES ('Bruyères', 88078, '48.2', '6.716667')
INSERT INTO [dbo].[VILLE_1]([LIB_VILLE], [CODE_INSEE], [LATITUDE], [LONGITUDE]) VALUES ('Champ-le-Duc', 88086, '48.2', '6.716667')
 
SELECT (acos(1)) AS A_COS,
	cos(radians(cast(isnull(v1.LATITUDE, '0') AS float)))
	*cos(radians(cast(isnull(v2.LATITUDE, '0') AS float)))
	*cos(radians(cast(isnull(v2.LONGITUDE, '0') AS float))
		-radians(cast(isnull(v1.LONGITUDE, '0') AS float))
        )
	+sin(radians(cast(isnull(v1.LATITUDE, '0') AS float)))
	*sin(radians(cast(isnull(v2.LATITUDE, '0') AS float))) AS VALEUR_CALCULEE
FROM VILLE_1 v1
	INNER JOIN VILLE_1 v2 ON v1.LIB_VILLE > v2.LIB_VILLE
WHERE isnull(v1.LATITUDE, '') <> ''
	AND isnull(v1.LONGITUDE, '') <> ''
	AND isnull(v2.LATITUDE, '') <> ''
	AND isnull(v2.LONGITUDE, '') <> ''
	AND abs(abs(cast(isnull(v1.LATITUDE, '0') AS float)) - abs(cast(isnull(v2.LATITUDE, '0') AS float))) < 1
	AND abs(abs(cast(isnull(v1.LONGITUDE, '0') AS float)) - abs(cast(isnull(v2.LONGITUDE, '0') AS float))) < 1
ORDER BY v1.LIB_VILLE, v2.LIB_VILLE 
 
SELECT (acos(
		cos(radians(cast(isnull(v1.LATITUDE, '0') AS float)))
		*cos(radians(cast(isnull(v2.LATITUDE, '0') AS float)))
		*cos(radians(cast(isnull(v2.LONGITUDE, '0') AS float))
			-radians(cast(isnull(v1.LONGITUDE, '0') AS float))
			)
		+sin(radians(cast(isnull(v1.LATITUDE, '0') AS float)))
		*sin(radians(cast(isnull(v2.LATITUDE, '0') AS float)))
		)
	) AS A_COS
FROM VILLE_1 v1
	INNER JOIN VILLE_1 v2 ON v1.LIB_VILLE <> v2.LIB_VILLE
WHERE isnull(v1.LATITUDE, '') <> ''
	AND isnull(v1.LONGITUDE, '') <> ''
	AND isnull(v2.LATITUDE, '') <> ''
	AND isnull(v2.LONGITUDE, '') <> ''
	AND abs(abs(cast(isnull(v1.LATITUDE, '0') AS float)) - abs(cast(isnull(v2.LATITUDE, '0') AS float))) < 1
	AND abs(abs(cast(isnull(v1.LONGITUDE, '0') AS float)) - abs(cast(isnull(v2.LONGITUDE, '0') AS float))) < 1
ORDER BY v1.LIB_VILLE, v2.LIB_VILLE
Y a-t-il un moyen de pallier cela autre que l'utilisation d'un case when? Je trouverais ça plutôt laid dans ce cas de figure.

Merci par avance.
__________________
Penser à la recherche et au bouton
franculo_caoulene est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 16/09/2008, 17h13   #2
Rédacteur/Modérateur

 
Avatar de SQLpro
 
Homme Frédéric BROUARD
Expert SGBDR & SQL
Inscription : mai 2002
Messages : 10 959
Détails du profil
Informations personnelles :
Nom : Homme Frédéric BROUARD
Localisation : France

Informations professionnelles :
Activité : Expert SGBDR & SQL
Secteur : Conseil

Informations forums :
Inscription : mai 2002
Messages : 10 959
Points : 17 792
Points : 17 792
Vous avez visiblement une erreur d'écart d'arrondis. Préférez utiliser le type DECIMAL avec la précision voulue. Je pense que vous aurez moins de soucis.

A +
__________________
Frédéric Brouard - SQLpro - ARCHITECTE DE DONNÉES - expert SGBDR et langage SQL
Site sur les SGBD relationnels et le langage SQL: http://sqlpro.developpez.com/
Expert Microsoft SQL Server - M.V.P. (Most valuable Professional) MS Corp.
Blog SQL, SQL Server, modélisation données : http://blog.developpez.com/sqlpro
http://www.sqlspot.com : modélisation, conseils, audit, optimisation, formation
* * * * * Enseignant CNAM PACA - ISEN Toulon - CESI Aix en Provence * * * * *
SQLpro est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 19/09/2008, 16h33   #3
Expert Confirmé
 
Avatar de franculo_caoulene
 
Inscription : octobre 2003
Messages : 2 886
Détails du profil
Informations forums :
Inscription : octobre 2003
Messages : 2 886
Points : 2 559
Points : 2 559
Finalement, j'ai utilisé case... Mais merci pour le type decimal, je n'y avais jamais pensé.
__________________
Penser à la recherche et au bouton
franculo_caoulene est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 05h52.


 
 
 
 
Partenaires

Hébergement Web