Re: Converting integer + mantissa to float

[email protected] ("Jay Savage")
Newsgroups perl.beginners
Message-ID <[email protected]>
On Thu, Jun 12, 2008 at 4:45 AM, Dr.Ruud <[email protected]> wrote:
> "Jay Savage" schreef:
>
>> two integers as input that
>> represent a single float. The first is the integer part, the second is
>> hat mantissa. How do I recomine them into a single float?
>>
>> my $float = $int_part . '.' . $matissa; #or
>> my $float = sprintf "%u.%u", $int_part, $mantissa;
>
> The sprintf() makes me think that you receive the numbers, or at least
> the mantissa, with possible leading zeroes.
>
> I would add the venus operator:
>
> my $float = 0+ sprintf("%u.%u", $int_part, $mantissa);
>
> The $int_part is always non-negative?
>
> --
> Affijn, Ruud
>

You're right, the mantissa may have leading zeros. Good catch. I need
to keep them, though, or any later math will be off by multiple powers
of ten. I'd completely missed that on the first go-around, but what I
really need is

$x = sprinf "%u.%s", $int, $mantissa;

For the record, I'm retrieving high-res times for an external log
server (roughly equivalent to the output from
Time::HiRes::gettimeofday() ), and trying to put them back together
for use on our end. I guess I could also do something like:

$x = $int + ($mantissa / 10e6);

Thanks, again!

--j

--------------------------------------------------
This email and attachment(s): [ ] blogable; [ x ] ask first; [ ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org

values of β will give rise to dom!
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.