【问题标题】:Modulus in Pascal帕斯卡模数
【发布时间】:2015-08-28 05:16:15
【问题描述】:

我正在尝试将一些 Pascal 代码转换为 C++ 代码。我一直在试图弄清楚如何翻译这部分。

Function ThetaG_JD(jd : double) : double;
var
UT,TU,GMST : double;
begin
**UT   := Frac(jd + 0.5);**
jd   := jd - UT;
TU   := (jd - 2451545.0)/36525;
GMST := 24110.54841 + TU * (8640184.812866 + TU * (0.093104 - TU * 6.2E-6));
**GMST := Modulus(GMST + 86400.0*1.00273790934*UT,86400.0);**
ThetaG_JD := twopi * GMST/86400.0;
end; {Function ThetaG_JD}

我对我加粗的两行特别有问题。我怎样才能把它翻译成c++?非常感谢。

【问题讨论】:

  • 这似乎是一个函数调用,因为 Pascal 中的模数是 MOD 运算符。你有模数函数的来源吗?
  • 很遗憾我没有来源。我不明白为什么作者写出模数而不只是使用模数。但即使他这样做了,这些也不是整数,所以我认为 mod 不合适?
  • 你可以假设它是mod 的浮点变体,它会翻译为fmod(),但不要引用我的话 ;-) 同样Frac() 可能只返回小数部分,但实际上这是任何人的猜测。如果你没有所有的源代码,你实际上就无法正确地完成这项工作。
  • 您发布的代码调用了 modulus 函数,您的问题中没有包含该函数。由于它不是标准的 Pascal 函数(mod 运算符在 Pascal 中进行模运算)并且我们不知道 modulus 函数做什么或试图做什么,所以我们无法解释为什么它不能像你一样工作预计。找到模函数的来源(或至少是文档),我们可能会提供帮助。如果没有这些信息,您可能只能自己调试,因为我们没有能力这样做。

标签: c++ pascal modulus


【解决方案1】:

在 C++ 中,等效函数为:

fmod获取浮点模数

modf 将浮点项分解为其分数和整数部分(相当于Frac)。

【讨论】:

  • 我现在正在尝试理解 modf。是否可以用 'ut = modf(jd + .5, &fracPart);' 之类的内容替换该行?
  • 差不多,第二个参数是整数部分,所以你应该将你的虚拟变量命名为intPartut = modf(jd + 0.5, &intPart);
【解决方案2】:

如果你想计算儒略日格林威治平均恒星时本地平均恒星时,也许下面的内容可以帮助你 - 写在 PowerShell 中:

<#
    .Synopsis
    Astronomy calculations

    .Description
    Some helper functions to calculate:
    - Julian Day,
    - Greenwich Mean Sidereal time,
    - Local Mean Sidereal Time.
#>
cls

# https://en.wikipedia.org/wiki/Julian_day#Julian_day_number_calculation
function Get-JulianDay
{
    param ( [System.DateTime]$dt )
    $year = $dt.Year
    $month = $dt.Month
    $day = $dt.Day
    $hour = $dt.Hour
    $minute = $dt.Minute
    $second = $dt.Second
    $a = [System.Math]::Floor((14 - $month) / 12)
    $y = $year + 4800 - $a
    $m = $month + 12 * $a - 3
    $JDN = $day + [System.Math]::Floor((153 * $m + 2) / 5) + 365 * $y + [System.Math]::Floor($y / 4) - [System.Math]::Floor($y / 100) + [System.Math]::Floor($y / 400) - 32045
    $JD = $JDN + ($hour - 12) / 24 + $minute / 1440 + $second / 86400
    return ($JD)
}

# https://en.wikipedia.org/wiki/Sidereal_time#Definition
# http://aa.usno.navy.mil/faq/docs/GAST.php
function Get-GMST
{
    param ( [double]$JD )
    $D = $JD - 2451545.0
    $GMST = 18.697374558 + 24.06570982441908 * $D
    return ($GMST % 24)
}

function Get-LMST
{
    param ( [double]$gmst, [double]$longitude )
    return ( $gmst + $longitude / 15.0 )
}

# Test above functions
$current = (Get-Date).ToUniversalTime()
$jd = Get-JulianDay -dt $current
$gmst = Get-GMST -JD $jd
$longitude = 17.668487800
$lmst = Get-LMST -gmst $gmst -longitude $longitude
$lst = [timespan]::FromHours($lmst).ToString()
Write-Host "Local mean sidereal time: $lst"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-28
    • 2017-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多