找回密码
 快速注册
搜索
查看: 15|回复: 0

[地理]计算地球上任意两点(经纬度)之间的距离

[复制链接]

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

hbghlyj 发表于 2024-9-12 23:28 |阅读模式
问题是:
  1. text = 'Write a python function that calculates the distance between any two latitudes and longitudes on earth'
复制代码

首先我们询问 Gemini(最后一句要求它不要生成额外的文本)
  1. # ask Gemini
  2. model = genai.GenerativeModel('gemini-pro')
  3. chat = model.start_chat(history=[])
  4. response = chat.send_message('%s -- Please answer as concisely as you can, avoiding any extra conversation or text' % text)
  5. gemini_response = response.text
复制代码

他的回答是
  1. import math
  2. def distance(lat1, lon1, lat2, lon2):
  3.   """Calculates the distance between two latitudes and longitudes.
  4.   Args:
  5.     lat1: The latitude of the first point.
  6.     lon1: The longitude of the first point.
  7.     lat2: The latitude of the second point.                       
  8.     lon2: The longitude of the second point.                       
  9.        
  10.   Returns:
  11.     The distance between the two points in kilometers.
  12.   """
  13.   # Convert the latitudes and longitudes to radians.
  14.   lat1 = math.radians(lat1)                       
  15.   lon1 = math.radians(lon1)                       
  16.   lat2 = math.radians(lat2)                       
  17.   lon2 = math.radians(lon2)                       
  18.                        
  19.   # Calculate the difference between the latitudes and longitudes.                       
  20.   dlat = lat2 - lat1                       
  21.   dlon = lon2 - lon1                       
  22.                        
  23.   # Calculate the square of the sine of the half of the difference in latitude.                       
  24.   sin_dlat_half = math.sin(dlat / 2) ** 2                       
  25.                        
  26.   # Calculate the square of the sine of the half of the difference in longitude.                       
  27.   sin_dlon_half = math.sin(dlon / 2) ** 2                       
  28.                        
  29.   # Calculate the square of the great-circle distance between the two points.                       
  30.   a = sin_dlat_half + math.cos(lat1) * math.cos(lat2) * sin_dlon_half                       
  31.                        
  32.   # Calculate the great-circle distance between the two points.                       
  33.   c = 2 * math.asin(math.sqrt(a))                       
  34.                        
  35.   # Calculate the Earth's radius in kilometers.                       
  36.   r = 6371                       
  37.                        
  38.   # Calculate the distance between the two points in kilometers.                       
  39.   distance = r * c                       
  40.                        
  41.   return distance
复制代码
然后我们询问 ChatGPT(最后一句要求它不要生成额外的文本)
  1. # ask ChatGPT
  2. completion = client.chat.completions.create(
  3.   model="gpt-3.5-turbo",
  4.   messages=[
  5.     {"role": "user", "content": '%s -- Please answer as concisely as you can, avoiding any extra conversation or text' % text}
  6.   ]
  7. )
  8. openai_response = completion.choices[0].message.content
复制代码

他的回答是
  1. import math
  2. def calculate_distance(lat1, lon1, lat2, lon2):
  3.     lat1 = math.radians(lat1)
  4.     lon1 = math.radians(lon1)
  5.     lat2 = math.radians(lat2)
  6.     lon2 = math.radians(lon2)
  7.     # Calculate differences
  8.     dlat = lat2 - lat1
  9.     dlon = lon2 - lon1
  10.     # Calculate distance using Haversine formula
  11.     a = math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2
  12.     c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
  13.     distance = 6371 * c  # Earth's radius in kilometers
复制代码

这两个生成的脚本哪个正确?

手机版|悠闲数学娱乐论坛(第3版)

GMT+8, 2025-3-4 12:15

Powered by Discuz!

× 快速回复 返回顶部 返回列表