Unexpected output while calling octave script from php

萝らか妹 提交于 2019-12-11 13:43:05

问题


I am calling Octave script from PHP and passing parameters to it. But I get the unexpected output.

The PHP code I'm using to pass arguments and call the Octave script is:

$a=8;
$b=3;
$cmd = "C:\Octave\Octave4\bin\octave-cli C:\wamp\www\dspace\add.m $a $b";
$ex = passthru($cmd, $op);
var_dump($ex);

My Octave script:

arglist = argv();
 a = arglist{1};
 b = arglist{2};

function f (a,b)
  a + b
endfunction

printf(f(a,b));

The output get is:

ans = 107

Expected output is:

11

How can I fix this?


回答1:


You are adding the ASCII codes for the character "8" which is 56 and "3" which is 51 so the result is 107. Convert the strings to numbers:

arglist = argv();

a = str2num (arglist{1});
b = str2num (arglist{2});

function ret = f (a,b)
  ret = a + b;
endfunction

printf("%i", f(a,b));


来源:https://stackoverflow.com/questions/36934436/unexpected-output-while-calling-octave-script-from-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!