Yii Framework Forum: How take float fraction part? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How take float fraction part? Rate Topic: -----

Poll: How take float fraction part? (4 member(s) have cast votes)

Which code is better for your opinion?

  1. $fract = $float - floor($float); (1 votes [25.00%])

    Percentage of vote: 25.00%

  2. $fract = fmod($float, 1); (2 votes [50.00%])

    Percentage of vote: 50.00%

  3. other (explain with a post) (1 votes [25.00%])

    Percentage of vote: 25.00%

Vote Guests cannot vote

#1 User is offline   voice 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 05-October 09

Posted 13 December 2010 - 05:39 PM

Hi.

We have a little discussion here about next two code snippets.
Please, help us and take part in this pool. :)

Thanks.
0

#2 User is offline   diggy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 112
  • Joined: 18-November 09
  • Location:Ukraine, Khmelnitsky

Posted 13 December 2010 - 05:44 PM

View Postvoice, on 13 December 2010 - 05:39 PM, said:

Hi.

We have a little discussion here about next two code snippets.
Please, help us and take part in this pool. :)

Thanks.


Hi guys,

The question there is not "how to take float fraction part", it's

what of these two code snippets are more elegant, effective and shorter

:)
0

#3 User is offline   voice 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 05-October 09

Posted 13 December 2010 - 05:53 PM

Not always shorter code means better code. And not length was the root of our discussion. Everyone can see that second variant is shorter, but this is not a question.
0

#4 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 13 December 2010 - 06:37 PM

Have no idea. :)
I would choose the second one simply because it looks cooler.:P
But: a very important question:
If fmod is more expensive than performing a subtraction and a floor, then I'd probably choose the first.
Maybe not ??
Any difference in the results? Like precision?
"Less noise - more signal"
0

#5 User is offline   voice 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 05-October 09

Posted 14 December 2010 - 01:52 AM

View Postjacmoe, on 13 December 2010 - 06:37 PM, said:

Any difference in the results? Like precision?

I think first result should be faster. Anyway, speed is not the goal in our case.
$ php -r '$pi = 3.14159; echo $pi - floor($pi), "\n";'
0.14159
$ php -r '$pi = 3.14159; echo fmod($pi, 1), "\n";'
0.14159
$ 

0

#6 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,342
  • Joined: 12-October 09
  • Location:Croatia

Posted 14 December 2010 - 02:57 AM

When adding a poll would be a good idea to offer more options... in this case "other (explain with a post)"...

Problem is if you have a negative number... then you should use
$fract = abs($float) - floor(abs($float));


And there is a problem that the floating point arithmetic is not 100% accurate, so if that is a problem you need to use BCMath like:
$float = 312.81304618290502673;
$fract = bcsub(abs($float),floor(abs($float)),20);


And there is always the option to use string functions like:
$float = 734.342947582635127;
$fract = strstr ( $float, '.' );


Edit:

Forgot one more way
$fract=$float-(int)$float;

Find more about me.... btw. Do you know your WAN IP?
0

#7 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 14 December 2010 - 03:46 AM

Maybe my question is dumb - but why didn't you simply benchmark which of your two suggestions is faster?
0

#8 User is offline   voice 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 05-October 09

Posted 14 December 2010 - 03:56 AM

View Postmdomba, on 14 December 2010 - 02:57 AM, said:

When adding a poll would be a good idea to offer more options... in this case "other (explain with a post)"...

Done.

mdomba, yes, you are right about negative numbers, we lost this case...

View Postmdomba, on 14 December 2010 - 02:57 AM, said:

Forgot one more way
$fract=$float-(int)$float;


Now this one my favourite. :)


View PostMike, on 14 December 2010 - 03:46 AM, said:

Maybe my question is dumb - but why didn't you simply benchmark which of your two suggestions is faster?

I don't sure that we can see a difference on such little piece of code. My idea is translate it into bytecode,
in this case the difference can be seen by the human eye.
0

#9 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 14 December 2010 - 04:02 AM

Of course you need to create a smart loop to measure the effect on a larger scale. Maybe check out this for some ideas:

http://www.phpbench.com/
0

#10 User is offline   voice 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 05-October 09

Posted 14 December 2010 - 04:32 AM

I mean that even with loop it will not give us real picture, I think.
0

#11 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 14 December 2010 - 04:34 AM

That's why i added the word "smart" in my statement above ... ;)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users