Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Julia函数将秒转换为小时、分钟、秒时出现问题_Julia - Fatal编程技术网

Julia函数将秒转换为小时、分钟、秒时出现问题

Julia函数将秒转换为小时、分钟、秒时出现问题,julia,Julia,我为Julia准备了将秒转换为小时、分钟和秒的代码,但是当我运行它时,我只得到(0,0,0)作为输出。有人能告诉我这有什么问题吗 function convert_from_seconds(sec::Int64) hours = 0 minutes = 0 seconds = 0 time = (hours, minutes, seconds) if sec < 60 seconds = sec elseif sec &l

我为Julia准备了将秒转换为小时、分钟和秒的代码,但是当我运行它时,我只得到
(0,0,0)
作为输出。有人能告诉我这有什么问题吗

function convert_from_seconds(sec::Int64)
    hours = 0
    minutes = 0
    seconds = 0

    time = (hours, minutes, seconds)

    if sec < 60
        seconds = sec
    elseif sec < 3600
        minutes = floor(sec / 60)
        seconds = sec % 60
    elseif sec < 216000
        hours = floor(sec / 3600)
        minutes = floor(hours % 3600)
        seconds = minutes % 60
    end
    return time
end
函数从秒数转换秒数(sec::Int64)
小时=0
分钟=0
秒=0
时间=(小时、分钟、秒)
如果秒<60
秒=秒
埃尔塞夫秒<3600
分钟=楼层(秒/60)
秒=秒%60
埃尔塞夫秒<216000
小时=楼层(秒/3600)
分钟=楼层(小时数%3600)
秒=分钟%60
结束
返回时间
结束

方法稍有不同,我是Julia noob,但这似乎适用于我创建的测试用例,而且似乎有点简单。正如在评论中提到的,我认为您的问题是(a)创建
时间,而小时、分钟和秒是0,而不是在计算它们之后,(b)计算中的一些错误逻辑

function convert_from_seconds(sec::Int64)
    hours = 0
    minutes = 0
    seconds = sec % 60

    if sec >= 60
        minutes = floor(sec / 60)
    end
    if minutes >= 60
        hours = floor(minutes / 60)
        minutes = minutes % 60
    end
    time = (hours, minutes, seconds)
    return time
    end
我运行了这些测试,所有测试都按预期进行:

print(convert_from_seconds(0))
print(convert_from_seconds(1))
print(convert_from_seconds(59))
print(convert_from_seconds(60))
print(convert_from_seconds(61))
print(convert_from_seconds(120))
print(convert_from_seconds(121))
print(convert_from_seconds(7270))
print(convert_from_seconds(3600))
print(convert_from_seconds(3601))

以下是您可能需要考虑的另一种方法:

function convert_from_seconds(sec::Int)
    x, seconds = divrem(sec, 60)
    hours, minutes = divrem(x, 60)
    hours, minutes, seconds
end

Conisder使用标准的
Dates
API及其算法-非常棒!比编写自己的函数更优雅:

julia> using Dates

julia> s = Time(0) + Second(4000)
01:06:40

julia> hour(s), minute(s), second(s)
(1, 6, 40)

如果您考虑24小时或以上

,上述代码将不会给出期望的结果。
julia> s = DateTime(0) + Second(4*3600*24+4000)
0000-01-05T01:06:40

julia> year(s), (month(s)-1), (day(s)-1), hour(s), minute(s), second(s)
(0, 0, 4, 1, 6, 40)

没有零月零日,所以我们需要减去
1

作为一名软件工程师,您必须能够 处理任何有效的输入

function convert_from_seconds(sec::Int64)
    if sec == 0
        return (0,0,0)
    elseif  sec < 0
        positive_result = convert_from_seconds(-1 * sec)
        return (-1 * positive_result[1],-1 * positive_result[2],-1 * positive_result[3])
    else
        seconds = sec % 60
        mins    = sec ÷ 60
        minutes = mins % 60
        hours   = mins ÷ 60
        return (hours,minutes,seconds)
    end    
end 

println(  50  ," ",convert_from_seconds(   50 ))
println( 100  ," ",convert_from_seconds(  100 ))
println( 2050 ," ",convert_from_seconds( 2050 ))
println( 4050 ," ",convert_from_seconds( 4050 ))
println( -50  ," ",convert_from_seconds(  -50 ))
println(-100  ," ",convert_from_seconds( -100 ))
println(-2050 ," ",convert_from_seconds(-2050 ))
println(-4050 ," ",convert_from_seconds(-4050 ))
现在我将演示为什么我不是一名软件工程师。Int64的最小值为-2^63(-1*big(2)^63=-9223372036854775808),Int64的最大值为2^63-1(big(2)^63-1==9223372036854775807)


julia用户还不多,但是如果在计算完所有正确的值之后而不是之前放置
time=(小时、分钟、秒)
,该怎么办呢?另外,
minutes=floor(小时%3600)
这行看起来像个bug。你不能仅仅用小时来计算分钟。应该是
楼层((第3600节)/60)
。类似地,在所有情况下,
seconds=sec%60
都无法从分钟计算秒数,即使超过3600秒。如果您想使用日期-时间结构,这是一种非常好的推荐方法。当您只想进行转换时,秒的负值会有问题(因为它将从24:00:00开始倒计时)。好的,这很有意义,谢谢。是的,我知道你所说的秒数是
sec%60
。谢谢您也可以用于地板分割。
Starting Julia...
           _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.0.3 (2018-12-18)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

50 (0, 0, 50)
100 (0, 1, 40)
2050 (0, 34, 10)
4050 (1, 7, 30)
-50 (0, 0, -50)
-100 (0, -1, -40)
-2050 (0, -34, -10)
-4050 (-1, -7, -30)
julia> convert_from_seconds( -9223372036854775808 )
ERROR: StackOverflowError:
Stacktrace:
 [1] convert_from_seconds(::Int64) at ./untitled-7674d416d9e0628b948f1f3731dd1ecc:5 (repeats 80000 times)